File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

error handling

Artemesia Lakener
Ranch Hand

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

try {

try {
} catch (IOException e) {
handle it;
} 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)

Joined: Jun 27, 2002
Posts: 4118

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?

Java Regular Expressions
I agree. Here's the link:
subject: error handling