jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes Class Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Class Exception" Watch "Class Exception" New topic
Author

Class Exception

Vasco Dion�sio
Greenhorn

Joined: Dec 04, 2003
Posts: 22
Hi there,
I could use some help, please?
Here goes the code first and my problem after:
public void doSomething() throws IOException, Exception {
try {
//do something that throws an unknow exception (not IOException)
} catch (IOException e) {
System.out.println("IOException");
throw e;
} catch (Exception e) {
System.out.println("Exception");
throw e;
} finally {
System.out.println("finally");
}
}
When I call doSomething() an error occurs and the only output I see is:
finally. Shouldn't I always see the output
Exception
Finally
no matter what exception is thrown? Isn't Exception the SuperClass of all Exceptions?
Thanks a lot
[ January 14, 2004: Message edited by: Vasco Dion�sio ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Exception is the superclass of all Exceptions, but Throwable is the superclass of Exception and Error, the two main categories of exceptions (note that I
used the lowercase word "exceptions.") Error includes things like OutOfMemoryError, StackOverflowError, ArithmeticError, etc; they're unchecked, like RuntimeExceptions, but they correspond to VM errors rather than to programmer errors per se.
If you change the "Exception" to "Throwable", I believe you'll see the printout from that block.


[Jess in Action][AskingGoodQuestions]
Vasco Dion�sio
Greenhorn

Joined: Dec 04, 2003
Posts: 22
Hello,
First off all thanks for your answer...
You were right... it worked. Furthermore do you know the diference between Errors and Exceptions. Why do they exist both?
Thanks again
Maarten Vergouwen
Ranch Hand

Joined: Jan 12, 2004
Posts: 60
An Error is something that shouldnt happen, and if it does it is not something that you can do anything about once it happens. Things like 'out of memory' and stackoverflows are Errors.
Exceptions are things that can happen in a program, but that are not necessarily errors; they could well be the result of an entirely correct bit of logic. For example, 'File not found' is an exception, but wholly valid if your code was meant tot test for a file being present for example.
Likewise, when reading a file you often read until you get an 'end of file' exception, which means you can stop reading.
Some Exceptions _are_ errors, such as an Arithmetic exception. But these are things you can prevent in your code (ie checking a number to see if it's not 0 before dividing something by it).
[ January 14, 2004: Message edited by: Maarten Vergouwen ]
Vasco Dion�sio
Greenhorn

Joined: Dec 04, 2003
Posts: 22
Thanks a lot... both of you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class Exception
 
Similar Threads
Exceptions
Finally
catch block.....
Nested Try/Catch
(basic ) use of finally