File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception Doubt.. 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 "Exception Doubt.." Watch "Exception Doubt.." New topic

Exception Doubt..


Joined: Mar 15, 2005
Posts: 22
Question 1

If I write a code like the one below why does it not give a compilation error. If I change Exception to IOException it does throw a compilation error.

Question 2

If an IOException is thrown, then even an Exception can catch it because it is its super class. Then why not an Object.

Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2398
The exceptions that are subclasses of RuntimeException are unchecked. So by catching an Exception you are looking for a wide range of exceptions. But if you choose a specific checked exception, then you need to make sure there is some code in the try block that has the potential for throwing that exception.

The parameter in the catch block must be a subclass of Throwable.
Jeff Tian

Joined: Jan 06, 2005
Posts: 12
Hi KrishnaKumar.

Further to what Keith said,
I think question one also involves "implicit import" of java.lang.* library. I read somewhere that this happens, but I dunno much about it.

Note that Exception and other exceptions such as SecurityException, ArithmeticException are in the library and do not need any additional import declaration. See API.

If you want to use IOException, you have to import and you also have to put some code in try block that will possibly throw IOException eg

FileReader fr = new FileReader("someFileName.txt");

As for Question 2, only Throwable and subclass can be caught (Keith) and while an exception is an object an object is not an exception.
[ March 29, 2005: Message edited by: Jeff Tian ]

The magic of believing in yourself is to risk all that you have for a dream <br />that nobody sees but you. (Million Dollar Baby)<br /> <br />SCJP 1.4
I agree. Here's the link:
subject: Exception Doubt..
It's not a secret anymore!