• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception handling : after exception is caught i am able to print the next statements is it allowed  RSS feed

 
vinayak jog
Ranch Hand
Posts: 83
MySQL Database Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why Not ....
Did you read it somewhere else ???
 
vinayak jog
Ranch Hand
Posts: 83
MySQL Database Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikhil Sagar wrote:Why Not ....
Did you read it somewhere else ???


Give me links
 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when you say
i am able to print the next statements is it allowed

then what do you mean by NEXT STATEMENTS here ??
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vinayak jog wrote:


Does that compile? It looks like it will, and if it does, then obviously it's syntactically allowed. It's not a good way to write code though. Catching an exception doesn't fix the problem. It just transfers control to a block where you know that something has gone wrong, and what that something is, so that you can do something to fix it if possible. If your catch block isn't going to actually fix the problem (for instance, retry, or use some valid default value instead), then you shouldn't catch it in the first place, or if you do, you should wrap and rethrow it.

For instance, let's say your code is:


If there's an IOException, that means our code was unable to read the file, so, obviously, we were not able to populate the data structure, but we're just continuing on as if everything is fine. When we get to the process line, we'll have an invalid data structure.

That process line needs to go inside the try block, so we never reach if if there was a problem, or else we just shouldn't catch the exception at all.

 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume you have read the appropriate section in the Java Tutorials?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!