Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception Handling

 
kapil apshankar
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Consider the following code:
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" );
}

It prints:
Exception thrown
Done with exceptions
myMethod is done
Why does it print the last line? I thought finally is called before the method exits...Could someone help me please?
Or is it that the rest of the method is executed after handling the exception?
Regards,
Kapil
 
Vidya Krishnamurthy
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the Exception is caught, the method doesn't terminate and the statements following the finally block gets executed. if the exception is not caught, then the finally block gets executed and the method terminates. In your example, as the exception is caught, the lines following the finally block gets executed. Hope this helps
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic