title: what is the exact order of execution for try, catch and finally?
body: <p>In this java code,  </p>
  
  <pre><code>import java.io.IOException;
  
  public class Copy
  {
     public static void main(String[] args)
     {
        if (args.length != 2)
        {
           System.err.println("usage: java Copy srcFile dstFile");
           return;
        }
  
        int fileHandleSrc = 0;
        int fileHandleDst = 1;
        try
        {
           fileHandleSrc = open(args[0]);
           fileHandleDst = create(args[1]);
           copy(fileHandleSrc, fileHandleDst);
        }
        catch (IOException ioe)
        {
           System.err.println("I/O error: " + ioe.getMessage());
           return;
        }
        finally
        {
           close(fileHandleSrc);
           close(fileHandleDst);
        }
     }
  
     static int open(String filename)
     {
        return 1; // Assume that filename is mapped to integer.
     }
  
     static int create(String filename)
     {
        return 2; // Assume that filename is mapped to integer.
     }
  
     static void close(int fileHandle)
     {
        System.out.println("closing file: " + fileHandle);
     }
  
     static void copy(int fileHandleSrc, int fileHandleDst) throws IOException
     {
        System.out.println("copying file " + fileHandleSrc + " to file " +
                           fileHandleDst);
        if (Math.random() &lt; 0.5)
           throw new IOException("unable to copy file");
  
        System.out.println("After exception");
     }
  }
  </code></pre>
  
  <p>the output that I expect is  </p>
  
  <pre><code>copying file 1 to file 2
  I/O error: unable to copy file
  closing file: 1
  closing file: 2
  </code></pre>
  
  <p>However sometimes I get this expected output and at other times I get the following output:  </p>
  
  <pre><code>copying file 1 to file 2
  closing file: 1
  closing file: 2
  I/O error: unable to copy file  
  </code></pre>
  
  <p>and sometimes even this output:  </p>
  
  <pre><code>I/O error: unable to copy file
  copying file 1 to file 2
  closing file: 1
  closing file: 2
  </code></pre>
  
  <p>and whether I get the first, second or third output seems to happen randomly during every execution. I found <a href="https://stackoverflow.com/questions/512624/try-catch-block-in-java-execution-statements-in-catch-code">THIS POST</a> that apparently talks about the same problem, but I still don't understand why I sometimes get output 1, 2 or 3. If I understand this code correctly then output 1 should be what I get every time (the exception occurs). How do I ensure that I get output 1 consistently, or be able to tell when I will be getting output 1 or when I will 
  be getting output 2 or 3?</p>