File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Exception Handling

kapil apshankar
Ranch Hand

Joined: Dec 17, 2000
Posts: 66
Consider the following code:
void myMethod()
{ try
catch( NullPointerException npex )
System.out.println( "NullPointerException thrown " );
catch( Exception ex )
System.out.println( "Exception thrown " );
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?

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

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
jQuery in Action, 3rd edition
subject: Exception Handling
jQuery in Action, 3rd edition