• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception- pl help - exam tomorrow  RSS feed

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This is from a jqplus question:
class SomeThrowable extends Throwable { }
class MyThrowable extends SomeThrowable { }

public class TestClass
{
public static void main(String args[]) throws SomeThrowable
{
try{
m1();
}catch(SomeThrowable e){
System.out.println(e);
throw e;

}finally{
System.out.println("Done");
}
}
public static void m1() throws MyThrowable
{
throw new MyThrowable();
}
}
Ans : This prints Done followed by an Exception.
My Question: Here a checked exception is thrown ultimately in catch which is caught by putting a throws in the main method.
So it is as good as the default handler handling this exception.
So , there is no compile error, agreed but then why during runtime the print statement "Done" is followed by a RunTimeError (when the same has been caught already by the throws in the main method).
Am I losing out something on the concepts?
Also when we say 'throw e' it means we are throwing an object of somethrowable. In that case , are we trying to throw a runtimeexception or checkedException.
Pleas e help me. I have my exam tomorrow.
Thanks padmini
 
Sheriff
Posts: 12006
196
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You get the exception during runtime because of the "throw e;" statement in the catch block. You can see for yourself if SomeThrowable is a checked exception by removing "throws SomeThrowable" from main() and calling m1() without a try/catch. If it is a checked exception, the code will not compile. If it is an unchecked exception, the code will compile but you'll get a runtime exception.

 
We're all out of roofs. But we still have tiny ads:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!