The Artima Developer Community
Sponsored Link

Java Answers Forum
Program Flow: try, catch, finally

1 reply on 1 page. Most recent reply: Feb 12, 2004 6:31 AM by Fletcher Chambers

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Fletcher Chambers

Posts: 2
Nickname: chamfamdad
Registered: Feb, 2004

Program Flow: try, catch, finally Posted: Feb 12, 2004 6:19 AM
Reply to this message Reply
Advertisement
I was curious about whether the finally block or the exception block would execute first (I always assumed the exception block executed first), so I tried the following code:

class Finally
{
  public static void main(java.lang.String[] args)
  {
    try
    {
      System.out.println( "Throwing exception" );
 
      String sPointer = null;
      int i = sPointer.length();
 
      System.out.println( "Should never get here." );
    }
    catch( NullPointerException e )
    {
      System.err.println( "And very last of all: " + e );
    }
    finally
    {
      System.out.println( "Finally comes before exception block." );
    }
 
    System.out.println( "This gets executed before the catch!  Why is that?" );
 
  }
}


I discovered that the finally block is executed before the catch. But I was really surprised to see the code after the try\catch\finally executed before the catch block.

What is going on here??


Fletcher Chambers

Posts: 2
Nickname: chamfamdad
Registered: Feb, 2004

Re: Program Flow: try, catch, finally Posted: Feb 12, 2004 6:31 AM
Reply to this message Reply
Never mind. It seems my IDE is printing the statements in the wrong order. When I try this at a command prompt it works as expected.

Flat View: This topic has 1 reply on 1 page
Topic: a couple of newbie questions .. Previous Topic   Next Topic Topic: cannot resolve symbol problem

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use