31. Consider the code below: void myMethod() { try { fragile(); } catch( NullPointerException npex ) { System.out.println( "NullPointerException thrown " ); } catch( Exception ex ) { System.out.println( "Exception thrown " ); } finally { System.out.println( "Done with exceptions " ); } System.out.println( "myMethod is done" ); } What is printed to standard output if fragile() throws an IllegalArgumentException? a) "NullPointerException thrown" b) "Exception thrown" c) "Done with exceptions" d) "myMethod is done" e) Nothing is printed Is this question, I thought the exception throwed by fragile() was catched by the 2nd catch statement and then the finally statement executed.And then it jumps out of myMethod() So my answer was b & c. But the answer in the correct answer was b & c & d. What'd the problem?
Anthony Villanueva
Ranch Hand
Joined: Mar 22, 2002
Posts: 1055
posted
0
After a catch block has been executed, control is always transferred to the finally block, if one is specified... On exit from a finally block, normal execution will resume. (Mughal p.158-159)
Thomas Paul
mister krabs
Ranch Hand
Joined: May 05, 2000
Posts: 13974
posted
0
If you want it to jump out then you either use a return statement or don't catch the exception and declare a throws in your method definistion.