This week's book giveaway is in the JavaFX forum.
We're giving away four copies of Introducing JavaFX 8 Programming and have Herbert Schildt on-line!
See this thread for details.
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

Win a copy of Introducing JavaFX 8 Programming this week in the JavaFX forum!
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
I agree. Here's the link:
subject: Exception Handling
It's not a secret anymore!