Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

another question from MindQ Mock Exam

 
Ray Chang
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic