my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception Handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception Handling" Watch "Exception Handling" New topic
Author

Exception Handling

kapil apshankar
Ranch Hand

Joined: Dec 17, 2000
Posts: 66
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


Hope this helps. Correct me if I am wrong.<p>Cheers <img src="smile.gif" border="0"> ,<br />Kapil
Vidya Krishnamurthy
Greenhorn

Joined: Mar 27, 2001
Posts: 15
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
 
Consider Paul's rocket mass heater.
 
subject: Exception Handling
 
Similar Threads
Exception
Exceptions
MindQ Q31
another question from MindQ Mock Exam
Question related to exception