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

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -