• 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

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: 12542
206
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.

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!