aspose file tools*
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
Author

Exception

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?

1.

2.

3.

I imported java.io 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
Sheriff

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

Joined: Oct 27, 2005
Posts: 19693
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
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: 14146
    
  18

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 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception