package test; /** * A simple test program to show how try/finally exceptions are propagated. *

* This progam creates the following output: *

*
 * 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
 * 
*/ public class TryFinallyExceptionTest { /** */ public TryFinallyExceptionTest() { super(); } /** * A simple test method. * @param throwTryException true: throw exception in try block * @param throwFinallyException true: 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); } } }