aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes exceptions in try and catch statements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "exceptions in try and catch statements" Watch "exceptions in try and catch statements" New topic
Author

exceptions in try and catch statements

jason stark
Greenhorn

Joined: Dec 01, 2002
Posts: 10
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

Joined: Sep 23, 2000
Posts: 3312
    
    7
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 ]

Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
jason stark
Greenhorn

Joined: Dec 01, 2002
Posts: 10
thanks Paul that really makes sense of it
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: exceptions in try and catch statements