Forums Register Login

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

+Pie Number of slices to send: Send

--------
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?
+Pie Number of slices to send: Send
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).]
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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).]
+Pie Number of slices to send: Send
 

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
Always look on the bright side of life. At least this ad is really tiny:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1012 times.
Similar Threads
Exception doubt
similar question,but ans is different so much?
q from abhilash's mock exam on try catch
Why so much difference between two similar class?
Try catch block.....
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 05:43:00.