java - why not debug rather why exception handling -
we specify exception in try , catch.if know exception gonna generated why go exception handling rather debug part of code?
according oracle definition of exception
an exception event occurs during execution of program that disrupts normal flow of instructions.
conclusion:
if put try {} catch
block know trow exception, code wrong.
for example, code compiles? yes, wrong
string s = "throwme!"; try { int number = integer.parseint(s); } catch (numberformatexception) { }
correct exception use
system.out.println("enter number"); string s = scanner..... // user enters try { int number = integer.parseint(s); } catch (numberformatexception) { // tell user there problem } // tell user genius
this have 2 execution flows, correct 1 (user genius) in moment user enters value disrupting flow (integer.parseint(s);
) exception thrown...
Comments
Post a Comment