Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class Exception

 
Vasco Dion�sio
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vasco Dion�sio
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot... both of you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic