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 pls have a look at following two examples of the same kind of question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "pls have a look at following two examples of the same kind of question" Watch "pls have a look at following two examples of the same kind of question" New topic
Author

pls have a look at following two examples of the same kind of question

wei liu
Ranch Hand

Joined: Dec 06, 2001
Posts: 56

--------
Question 132)
public class AQuestion
{
public static void main(String args[])
{
System.out.println("Before Try");
try
{
}
catch(Throwable t)
{
System.out.println("Inside Catch");
}
System.out.println("At the End");
}
}


Compiler error complaining about the catch block, where no Throwable object can ever be thrown.
Compiler error - Throwable Object can not be caught, only Exceptions must be caught.
No compiler error. The lines "Before Try" and "At the end" are printed on the screen.
the ans is the 3rd. why?

--------------------------------------------------------------------------------
Question 133)
public class AQuestion
{
public static void main(String args[])
{
System.out.println("Before Try");
try
{
}
catch(java.io.IOException t)
{
System.out.println("Inside Catch");
}
System.out.println("At the End");
}
}
Compiler error complaining about the catch block where no IOException object can ever be thrown.
Compiler error - IOException not found. It must be imported in the first line of the code.
No compiler error. The lines "Before Try" and "At the end" are printed on the screen.
the ans is the 1st . why?
Suraj Berwal
Greenhorn

Joined: Dec 28, 2001
Posts: 26
there are actually two type of exceptions which can be thrown by any method- checked and unchecked exceptions.
checked exceptions are always checked by the compiler at compile time only.for such exceptions compiler checks if these are thrown by any method. also such methods are caught in enclosed try-catch blocks.
no such checks are performed by compiler for
unchecked exceptions.these are left to be handled by JVM at runtime.
here IOException is a checked exception, hence is checked by copiler.
Throwable is superclass of Exception class which includes both checked and unchecked exceptions.this object can catch an unchecked exception which compiler is not concerned with.hence no error is displayed.
regards,
Suraj Berwal
Originally posted by wei liu:

--------
Question 132)
public class AQuestion
{
public static void main(String args[])
{
System.out.println("Before Try");
try
{
}
catch(Throwable t)
{
System.out.println("Inside Catch");
}
System.out.println("At the End");
}
}


Compiler error complaining about the catch block, where no Throwable object can ever be thrown.
Compiler error - Throwable Object can not be caught, only Exceptions must be caught.
No compiler error. The lines "Before Try" and "At the end" are printed on the screen.
the ans is the 3rd. why?


[This message has been edited by Suraj Berwal (edited December 28, 2001).]


Suraj Berwal
Sun Certified Programmer for Java 2 Platform
wei liu
Ranch Hand

Joined: Dec 06, 2001
Posts: 56
thanks for reply,but i m afraid you havenot understood what i mean.i just want to know why there are two different running results for these two nearly similar codes.i know the difference is catch(java.lang.IOexception) and catch(Throwable t).why?
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
wei
What Suraj is trying to say is that in the first code the catch statement catches a Throwable object which means that the try statement must be able to throw a Throwable object (or any of its subclasses). Because any unchecked exception is a subclass of a Throwable object this is ok and there is no compiler error.
In the second piece of code the catch statement is specifically looking for an IOException (or any of its subclasses) and since one is not thrown the compiler complains.
hope that helps
------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Dave Vick (edited December 28, 2001).]


Dave
Suraj Berwal
Greenhorn

Joined: Dec 28, 2001
Posts: 26
Originally posted by Dave Vick:
wei
What Suraj is trying to say is that in the first code the catch statement catches a Throwable object which means that the try statement must be able to throw a Throwable object (or any of its subclasses). Because any unchecked exception is a subclass of a Throwable object this is ok and there is no compiler error.
In the second piece of code the catch statement is specifically looking for an IOException (or any of its subclasses) and since one is not thrown the compiler complains.
hope that helps

dave ur right actually i meant this only. hope this u would have cleared ur doubts a bit, wei.

------------------
Suraj Berwal
Sun Certified Programmer for Java 2 Platform
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: pls have a look at following two examples of the same kind of question