Source of TestFinally.java


  1: //TestFinally.java
  2: //Demonstrates the "finally" "clause".

  4: import java.io.*;

  6: class MyException extends Exception
  7: {
  8:     public MyException() {}
  9:     public MyException(String s)
 10:     {
 11:         super(s);
 12:     }
 13: }


 16: public class TestFinally
 17: {
 18:     static PrintWriter screen = new PrintWriter(System.out, true);

 20:     static void failure() throws MyException
 21:     {
 22:         screen.println("Entered failure() method.");
 23:         throw new NullPointerException();
 24:     }


 27:     public static void main(String[] args)
 28:     {
 29:         try
 30:         {
 31:             screen.println("\nEntered try block.");
 32:             failure();
 33:         }
 34:         catch(MyException me)
 35:         {
 36:             screen.println("Entered catch block with MyException.");
 37:         }
 38:         finally
 39:         {
 40:             screen.println("Entered finally block.");
 41:             screen.println("Uncaught Exception - NullPointerException");
 42:         }
 43:     }
 44: }