File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

exceptions in try and catch statements

 
jason stark
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don’t understand why the code below compiles: –

But when I change the catch statement it fails

If I change the catch statement to (Exception e) it also compiles and runs
Is there any rule here that explains this?
Thanks Jason

[ Jess added UBB [code] tags to preserve whitespace, check 'em out! ]
[ January 25, 2003: Message edited by: Jessica Sant ]
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3664
9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler is pretty strict on non-runtime exceptions. So whenever you try to catch one, it complains if no non-runtime exception can be thrown from the try block. It can determine this becuase non-runtime exceptions have to be declared in the method signatures.
However, for runtime exceptions (and Errors), it does not care much (and cannot find out about). And since RuntimeException extends from Exception, the compiler has to allow Exception ( and thus, Throwable too).
So the point is, you can have a catch(RuntimeException) anywhere, but to allow that, the compile has to allow the super classes of RuntimeException i.e Exception and Throwable too.
HTH,
Paul.
[ January 24, 2003: Message edited by: Paul Anil ]
 
jason stark
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Paul that really makes sense of it
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic