GeeCON Prague 2014*
The moose likes Java in General and the fly likes error handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "error handling" Watch "error handling" New topic
Author

error handling

Artemesia Lakener
Ranch Hand

Joined: Jun 21, 2005
Posts: 162
if I have --

try {

code_part_1;
try {
code_part_2;
} catch (IOException e) {
handle it;
}
code_part_3;
} catch(IOException e) {
handle it in another way differently;
}


Is it ok to do this ? what I really want to do is --
if code_part_2 causes IOException I want to deal with it specifically; if code_1 and code_3 causes IOException, I want to handle it in another way. Does my code suit my need ?
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
Artemesia,

You can't catch the same exception twice in a row: that's just not allowed. However, you could catch subclasses of the same exception twice: say first catch a FileNotfoundException, then a FileException. IMO, you should only do this if you're going to be taking different actions based on the type of exception: otherwise, you probably just want to catch the base class.

Make sense?
M


Java Regular Expressions
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: error handling