File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Catching exception question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Catching exception question" Watch "Catching exception question" New topic
Author

Catching exception question

Leandro Melo
Ranch Hand

Joined: Mar 27, 2004
Posts: 401
Hi, if try to catch something that cannot be thrown from anywhere, I'll have an unreachable code and an compile error. For instance, this code gives me a compile error.


I understand this fine. But when you don't use Exception subclasses, the following code compiles fine. In other words, if you try to catch Exception base class, it's fine, it doesn't generate compile errors.


Can anyone clarify this behaviour for me?


Leandro Melo
SCJP 1.4, SCWCD 1.4
Adam Czysciak
Ranch Hand

Joined: Feb 25, 2005
Posts: 82
Originally posted by Leandro Melo:
In other words, if you try to catch Exception base class, it's fine, it doesn't generate compile errors.


Can anyone clarify this behaviour for me?[/QB]


Sure ;-) You can catch any unchecked exceptions at any time. RuntimeException and it's subclasses are instanceof Exception, therefore you can catch it. The same applies to Error and its subclasses. And, obiously, you could also try to catch Throwable.


Adam<br /> <br /><i>SCJA, SCJP 5.0, SCWCD 1.4, SCBCD 5, SCEA 5</i>
Leandro Melo
Ranch Hand

Joined: Mar 27, 2004
Posts: 401
Sure! Thanks.
 
 
subject: Catching exception question
 
Similar Threads
Exception?
true or false?
About one Exception quesion,help
Checked Exception
Exception