Example of Try-Catch Blocks To Handle .: Numberformatexception

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 1

114-15.

Example of try-catch blocks to handle NumberFormatException.


public static void main(String[] args) { String[] in1={"Hello","23"," 2"!" String[] in2={"#ar Heel", "Strongbad", "123 "!" int count=$1" %% &teration counter' String s=""" int num1, num2" %% (ill )old integer version o* String' +)ile (true) { count,," S-stem'out'println("&teration ", count ,". input is ", in1[count] , " and ", in2[count])" tr{ s=in1[count]" num1=&nteger'parse&nt(s)" s=in2[count]" num2=&nteger'parse&nt(s)" %% &* +e get to t)is point, +e )ave success*ull- read 2 integers' %% /ispla- t)e numbers and leave t)e loop' S-stem'out'println("0umbers are ",num1," and ",num2,"'")" brea1" ! %% 2nd o* tr- bloc1 catc) (0umber3ormat24ception e) { S-stem'out'print("Sorr-, 5"",s,"5" is not an integer'")" S-stem'out'println(" #r- again")" S-stem'out'println("$6",e)" %% $6 added to identi*- t)is line' e'printStac1#race()" ! %% 2nd o* catc) 0umber3ormat24ception ! %% 2nd o* +)ile loop %% 2nd o* main

Output

$$$$789:S; e4ec. 7ava 24cept<lass &teration =. input is Hello and #ar Heel Sorr-, "Hello" is not an integer' #r- again $67ava'lang'0umber3ormat24ception. 3or input string. "Hello" 7ava'lang'0umber3ormat24ception. 3or input string. "Hello" at 7ava'lang'0umber3ormat24ception'*or&nputString(0umber3ormat24ception'7ava. >) at 7ava'lang'&nteger'parse&nt(&nteger'7ava. ?>) at 7ava'lang'&nteger'parse&nt(&nteger'7ava.@1>) at 24cept<lass'main(24cept<lass'7ava.1A) &teration 1. input is 23 and Strongbad Sorr-, "Strongbad" is not an integer' #r- again $67ava'lang'0umber3ormat24ception. 3or input string. "Strongbad" 7ava'lang'0umber3ormat24ception. 3or input string. "Strongbad" at 7ava'lang'0umber3ormat24ception'*or&nputString(0umber3ormat24ception'7ava. >) at 7ava'lang'&nteger'parse&nt(&nteger'7ava. ?>) at 7ava'lang'&nteger'parse&nt(&nteger'7ava.@1>) at 24cept<lass'main(24cept<lass'7ava.21) &teration 2. input is 2 and 123 0umbers are 2 and 123 ' $$$$789:S;. operation complete'

You might also like