maroph icon

Java: try/finally exception propagation

maroph | PRO | 03/28/14 01:41:18 PM UTC | 0 ⭐ | 308 👁️ | Never ⏰ | []
Java 5 |

0 B

|

None

|

0 👍

/

0 👎

package test;
 
/**
 * A simple test program to show how try/finally exceptions are propagated.
 * <p>
 * This progam creates the following output: 
 * </p>
 * <pre>
 * test #1: no exception
 * 
 * test #2: throw exception in try block
 * got expected exception from try block
 * 
 * test #3: throw exception in finally block
 * got expected exception from finally block
 * 
 * test #4: throw exception both in try and finally block
 * got expected exception from try finally
 * </pre>
 */
public class TryFinallyExceptionTest {
    /** */
    public TryFinallyExceptionTest() {
        super();
    }
 
    
    /**
     * A simple test method. 
     * @param throwTryException     <code>true</code>: throw exception in try block
     * @param throwFinallyException <code>true</code>: throw exception in finally block
     * @throws TryFinallyException  test exception to throw
     */
    public void testTryFinallyException(boolean throwTryException, boolean throwFinallyException)
           throws TryFinallyException {
        try {
            if (throwTryException) {
                throw new TryFinallyException("TRY");
            }
        }
        finally {
            if (throwFinallyException) {
                throw new TryFinallyException("FINALLY");
            }
        }
    }
 
 
    /**
     * @param args no arguments needed
     */
    public static void main(String[] args) {
        TryFinallyExceptionTest test = new TryFinallyExceptionTest();
 
        try {
            System.out.println("test #1: no exception");
            System.out.flush();
            test.testTryFinallyException(false, false);
        }
        catch (Exception e) {
            System.out.println(">>>>> unexpected exception!");
            e.printStackTrace(System.out);
            System.out.flush();
        }
        System.out.println("");
        System.out.flush();
        
        try {
            System.out.println("test #2: throw exception in try block");
            System.out.flush();
            test.testTryFinallyException(true, false);
        }
        catch (TryFinallyException e) {
            if (!"TRY".equals(e.getMessage())) {
                System.out.println(">>>>> unexpected exception!");
                e.printStackTrace(System.out);
                System.out.flush();
            } else {
                System.out.println("got expected exception from try block");
            }
        }
        catch (Exception e) {
            System.out.println(">>>>> unexpected exception!");
            e.printStackTrace(System.out);
            System.out.flush();
        }
        System.out.println("");
        System.out.flush();
        
        try {
            System.out.println("test #3: throw exception in finally block");
            System.out.flush();
            test.testTryFinallyException(false, true);
        }
        catch (TryFinallyException e) {
            if (!"FINALLY".equals(e.getMessage())) {
                System.out.println(">>>>> unexpected exception!");
                e.printStackTrace(System.out);
                System.out.flush();
            } else {
                System.out.println("got expected exception from finally block");
            }
        }
        catch (Exception e) {
            System.out.println(">>>>> unexpected exception!");
            e.printStackTrace(System.out);
            System.out.flush();
        }
        System.out.println("");
        System.out.flush();
        
        try {
            System.out.println("test #4: throw exception both in try and finally block");
            System.out.flush();
            test.testTryFinallyException(true, true);
        }
        catch (TryFinallyException e) {
            if ("TRY".equals(e.getMessage())) {
                System.out.println("got unexpected exception from try block");
            } else if ("FINALLY".equals(e.getMessage())) {
                System.out.println("got expected exception from try finally");
            } else {
                System.out.println(">>>>> unexpected exception!");
                e.printStackTrace(System.out);
                System.out.flush();
            }
        }
        catch (Exception e) {
            System.out.println(">>>>> unexpected exception!");
            e.printStackTrace(System.out);
            System.out.flush();
        }
    }
    
 
    /**
     * A simple test exception.
     */
    @SuppressWarnings("serial")
    public static class TryFinallyException extends Exception {
        /** @param message X */
        public TryFinallyException(String message) {
            super(message);
        }
    }
}

Comments