File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Common Catches Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Common Catches" Watch "Common Catches" New topic
Author

Common Catches

Nick Delauney
Ranch Hand

Joined: Sep 28, 2002
Posts: 43
hello all,
quick question, If I have two IOExceptions, but I want to differentiate which IO excpetion was thrown so that I could print out two different messages, How would I do that ? Do I have to have a second try ?
try
{
throws IOException // First one
throws IOException // Second one
}catch(IOException ioe)
{
System.out.println(" IO Exception happened, no clue which one");
}
Thanks,


N.D:"Anything worth having, takes time to get"
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Use two or more catch blocks. Be sure to list the more specific Exceptions first (that is subclasses of the checked Exception). For Example:


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Nick Delauney
Ranch Hand

Joined: Sep 28, 2002
Posts: 43
Thanks Morris,
But, I'm wondering how would you differentiate between two of the same exceptions. In other words two IO EXceptions get thrown. How would I do a print statement to say which one?
Keep in mind there just IO Exceptions not anything derived from it but expicitly that type of exception.
How do you differentiate if the same expecption can be thrown two different ways in one try.
[ May 29, 2003: Message edited by: Nick Delauney ]
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

If both exceptions are of type IOException (and noot any subclass), then you would have 1 catch black with an if/else statement inside. The if would have to check some value of the exception.

Although, in general, testing the .getMessage() is proabably a bad idea (it might change over time...)


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
But, I'm wondering how would you differentiate between two of the same exceptions.
Well the safest approach would be to seperate the risky I/O statements into their own try blocks. You can also try what Joel posted but as he said, that is not a sure thing.
Nick Delauney
Ranch Hand

Joined: Sep 28, 2002
Posts: 43
Thank you both
Rodrigo Dinis
Ranch Hand

Joined: May 28, 2003
Posts: 40
Hello,
You must see the inheritance of your exception class and put the catches in the follow order: subclasses before and superclasses after, ex:
try {
/// IO code
} catch( EOFException e ) {
} catch( FileNotFoundException e ) {
} catch( IOException e ) {
}
Consider that EOFException and FileNotFoundException extends IOException.


Rodrigo Dinis
SCEA
SCJWSD
SCJD
SCBCD
SCWCD
SCJP
CNA
Brasilia - BRASIL
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Common Catches