Saturday, February 1, 2014

Example of a try catch finally block where finally is not run.

Source:
// Example of finally clause that does not run
public class FinallyNotRun {
   public static void main(String[] args) {
      try {
        System.out.println("Inside try");

        // should throw exception
        int x = 10 / 0;

      } catch (Exception e) {
        System.out.println("Inside catch");

        // Lets pretend to crash
        System.exit(0);

      } finally {
        // Will not run
        System.out.println("Inside finally");
      }
   }
}

Output:
   $ java FinallyNotRun 
   Inside try
   Inside catch