This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Journey To Enterprise Agility and have Daryl Kulak & Hong Li on-line!
See this thread for details.
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

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: 12198
199
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.

 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!