my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exceptions" Watch "Exceptions" New topic
Author

Exceptions

Sim Kim
Ranch Hand

Joined: Aug 06, 2004
Posts: 268


Why the above code compiles fine and why the one below gives compiler error ?

Saket Barve
Ranch Hand

Joined: Dec 19, 2002
Posts: 229
Lavjeet,

Exceptions are basically classified as: Checked Exceptions and Unchecked Exceptions.

Checked Exceptions may or may not be user-defined and, as such, need to be handled by your code.

Unchecked Exceptions are basically thrown and handled by the complier. You may or may not handle these.

Your first program:
You are catching Exception e. In other words, you are catching any of the RunTimeExceptions - Unchecked Exceptions - that are likely to occur. Since you are not expected to handle these (but you may) your code works fine.

Your second program:
You are catching Exception1 e, which is never thrown in your code. Realize that Exception1 is actually a checked exception which must be thrown *AND* handled by your code.

Here is one way to get it working:




Regards,
Saket
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exceptions