aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception question! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception question!" Watch "Exception question!" New topic
Author

Exception question!

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
What will be the result of compiling and running the following program ?
class NewException extends Exception {}
class AnotherException extends Exception {}
public class ExcceptionTest
{
public static void main(String [ ] args ) throws Exception
{
try
{
m2();
}
finally{ m3(); }
}
public static void m2() throws NewException{ throw new NewException(); }
public static void m3() throws AnotherException{ throw new AnotherException(); }
}

It throws AnotherException .. I ran the program in the compiler but I still don't get why it would just skip over the NewException and throw AnotherException .. the compiler doesn't throw more than 1 checked exception - is that true? I didn't think about that .. can someone clarify this?
Thanks!
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Eric,
The JVM can throw many exceptions (1 at a time) as long as your application handles them. In your case your finally block will always run unless you have called System.exit() in the try or catch block. Since your try method throws a newException it will perform the finally block before throwing the newException. Then your finally block throws a AnotherException. That exception is not handled in your try block so it is thrown up out of main. Your application is done because it didn't handle the exception.
You are correct in saying that a method can only throw one exception outside of itself each time it is called...
The JVM is not skipping your newException and just to be sure you can add a catch block just above your finally block and print out the results!
Regards,
Manfred.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Wait a second .. when finally throws AnotherException .. and there isn't a catch block for that exception (or the super-class) doesn't it check the throws clause of main next? The main method throws the superclass Exception ..
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Eric,
I think what's happening is that NewException() gets thrown, there's nothing to catch it but you've got a finally which is always executed; regardless of how the code ends, and since it calls m3(), which throws AnotherException, NewException is lost and only AnotherException is displayed.
If you comment out the call to m3() in the finally block, the runtime will display NewException.
JLS §14.19.2 states:
"If the run-time type of V is not assignable to the parameter of any catch clause of the try statement, then the finally block is executed. Then there is a choice:

  • If the finally block completes normally, then the try statement completes abruptly because of a throw of the value V.
  • If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S (and the throw of value V is discarded and forgotten)."

  • Hope that helps.
    ------------------
    Jane Griscti
    Sun Certified Programmer for the Java� 2 Platform


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
That's right!

Exactly my point. Your main is throwing the subclass exception: AnotherException out to the JVM. Which means that you are done!
Manfred.
 
 
subject: Exception question!