maroph

Java: try/finally exception propagation

Mar 28th, 2014
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 4.68 KB | None | 0 0
  1. package test;
  2.  
  3. /**
  4.  * A simple test program to show how try/finally exceptions are propagated.
  5.  * <p>
  6.  * This progam creates the following output:
  7.  * </p>
  8.  * <pre>
  9.  * test #1: no exception
  10.  *
  11.  * test #2: throw exception in try block
  12.  * got expected exception from try block
  13.  *
  14.  * test #3: throw exception in finally block
  15.  * got expected exception from finally block
  16.  *
  17.  * test #4: throw exception both in try and finally block
  18.  * got expected exception from try finally
  19.  * </pre>
  20.  */
  21. public class TryFinallyExceptionTest {
  22.     /** */
  23.     public TryFinallyExceptionTest() {
  24.         super();
  25.     }
  26.  
  27.    
  28.     /**
  29.      * A simple test method.
  30.      * @param throwTryException     <code>true</code>: throw exception in try block
  31.      * @param throwFinallyException <code>true</code>: throw exception in finally block
  32.      * @throws TryFinallyException  test exception to throw
  33.      */
  34.     public void testTryFinallyException(boolean throwTryException, boolean throwFinallyException)
  35.            throws TryFinallyException {
  36.         try {
  37.             if (throwTryException) {
  38.                 throw new TryFinallyException("TRY");
  39.             }
  40.         }
  41.         finally {
  42.             if (throwFinallyException) {
  43.                 throw new TryFinallyException("FINALLY");
  44.             }
  45.         }
  46.     }
  47.  
  48.  
  49.     /**
  50.      * @param args no arguments needed
  51.      */
  52.     public static void main(String[] args) {
  53.         TryFinallyExceptionTest test = new TryFinallyExceptionTest();
  54.  
  55.         try {
  56.             System.out.println("test #1: no exception");
  57.             System.out.flush();
  58.             test.testTryFinallyException(false, false);
  59.         }
  60.         catch (Exception e) {
  61.             System.out.println(">>>>> unexpected exception!");
  62.             e.printStackTrace(System.out);
  63.             System.out.flush();
  64.         }
  65.         System.out.println("");
  66.         System.out.flush();
  67.        
  68.         try {
  69.             System.out.println("test #2: throw exception in try block");
  70.             System.out.flush();
  71.             test.testTryFinallyException(true, false);
  72.         }
  73.         catch (TryFinallyException e) {
  74.             if (!"TRY".equals(e.getMessage())) {
  75.                 System.out.println(">>>>> unexpected exception!");
  76.                 e.printStackTrace(System.out);
  77.                 System.out.flush();
  78.             } else {
  79.                 System.out.println("got expected exception from try block");
  80.             }
  81.         }
  82.         catch (Exception e) {
  83.             System.out.println(">>>>> unexpected exception!");
  84.             e.printStackTrace(System.out);
  85.             System.out.flush();
  86.         }
  87.         System.out.println("");
  88.         System.out.flush();
  89.        
  90.         try {
  91.             System.out.println("test #3: throw exception in finally block");
  92.             System.out.flush();
  93.             test.testTryFinallyException(false, true);
  94.         }
  95.         catch (TryFinallyException e) {
  96.             if (!"FINALLY".equals(e.getMessage())) {
  97.                 System.out.println(">>>>> unexpected exception!");
  98.                 e.printStackTrace(System.out);
  99.                 System.out.flush();
  100.             } else {
  101.                 System.out.println("got expected exception from finally block");
  102.             }
  103.         }
  104.         catch (Exception e) {
  105.             System.out.println(">>>>> unexpected exception!");
  106.             e.printStackTrace(System.out);
  107.             System.out.flush();
  108.         }
  109.         System.out.println("");
  110.         System.out.flush();
  111.        
  112.         try {
  113.             System.out.println("test #4: throw exception both in try and finally block");
  114.             System.out.flush();
  115.             test.testTryFinallyException(true, true);
  116.         }
  117.         catch (TryFinallyException e) {
  118.             if ("TRY".equals(e.getMessage())) {
  119.                 System.out.println("got unexpected exception from try block");
  120.             } else if ("FINALLY".equals(e.getMessage())) {
  121.                 System.out.println("got expected exception from try finally");
  122.             } else {
  123.                 System.out.println(">>>>> unexpected exception!");
  124.                 e.printStackTrace(System.out);
  125.                 System.out.flush();
  126.             }
  127.         }
  128.         catch (Exception e) {
  129.             System.out.println(">>>>> unexpected exception!");
  130.             e.printStackTrace(System.out);
  131.             System.out.flush();
  132.         }
  133.     }
  134.    
  135.  
  136.     /**
  137.      * A simple test exception.
  138.      */
  139.     @SuppressWarnings("serial")
  140.     public static class TryFinallyException extends Exception {
  141.         /** @param message X */
  142.         public TryFinallyException(String message) {
  143.             super(message);
  144.         }
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment