File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Exception" Watch "Exception" New topic


vinod chemmi
Ranch Hand

Joined: Jun 15, 2009
Posts: 35

I have some doubt in exception handling.
As we know Exception,FileNotFoundException and IOException are all checked exceptions. right?
But in the following code first exception catching not display any compiler error but other two. why?




I imported package in the source. But i am confused by the behaviour that no compiler error for catching Exception class even if it is not generating any such exception in code block.
Any comments??

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46336
Maybe because Exception has unchecked subclasses, whereas IOException and FileNotFoundException only have checked subclasses.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20271

You can only catch exceptions that can actually be thrown. System.out.println doesn't throw an IOException or FileNotFoundException. Since any method can potentially throw any RuntimeException and Exception is the super class of RuntimeException, it's always possible to try to catch any RuntimeException or Exception itself. For the same reason, catching Throwable or Error (although not recommended) is always allowed.

How To Ask Questions How To Answer Questions
vinod chemmi
Ranch Hand

Joined: Jun 15, 2009
Posts: 35
Hi Experts

As every body knows Exception is a super class of several checked and unchecked exceptions.But how can conclude i should not catch exceptions if it is not generating in the code block? Obviously unchecked need not want any discuss , because it crystal clear. But in case of checked exceptions some of them allow us to include it even it not generating and some of them in other way. How to make a conclusion?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15081

If you want to know the exact details, then you can look it up in the Java Language Specification.

Section 11.2 Compile-Time Checking of Exceptions explains the exact rules.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
I agree. Here's the link:
subject: Exception
jQuery in Action, 3rd edition