javaclass

tests/ExceptionTest.java

139:9812fa1e4816
2005-01-21 Paul Boddie Worked on the documentation somewhat.
     1 public class ExceptionTest {     2      3     public int testThrow(int x) throws java.lang.Exception {     4         if (x == 0) {     5             throw new MyException();     6         } else if (x == 1) {     7             throw new MyOtherException();     8         } else {     9             return 1;    10         }    11     }    12     13     public int testCatch(int x) {    14         try {    15             if (x == 0) {    16                 throw new MyException();    17             } else if (x == 1) {    18                 throw new MyOtherException();    19             } else {    20                 x = 1;    21             }    22         } catch (MyException exc) {    23             x = 3;    24         } catch (MyOtherException exc) {    25             x = 2;    26         }    27         return x;    28     }    29     30     public int testIncomingCatch(int x) {    31         try {    32             return testThrow(x);    33         } catch (MyException exc) {    34             return 3;    35         } catch (MyOtherException exc) {    36             return 2;    37         } catch (java.lang.Exception exc) {    38             return -1; // Should be unreachable, really.    39         }    40     }    41     42     public int testFinally(int x) throws java.lang.Exception {    43         try {    44             if (x == 0) {    45                 x = 3;    46                 throw new MyException();    47             } else if (x == 1) {    48                 x = 2;    49                 throw new MyOtherException();    50             }    51             x = 1;    52         } finally {    53             x += 10;    54         }    55         return x;    56     }    57     58     public int testCatchFinally(int x) throws MyUncheckedException {    59         try {    60             if (x == 0) {    61                 throw new MyException();    62             } else if (x == 1) {    63                 throw new MyOtherException();    64             } else if (x == 2) {    65                 throw new MyUncheckedException();    66             }    67             x = 1;    68         } catch (MyException exc) {    69             x = 3;    70         } catch (MyOtherException exc) {    71             x = 2;    72         } finally {    73             x += 10;    74         }    75         return x;    76     }    77     78     public int testMultipleCatch(int x, int y) {    79         try {    80             x = testThrow(x);    81         } catch (MyException exc) {    82             x = 3;    83         } catch (MyOtherException exc) {    84             x = 2;    85         } catch (java.lang.Exception exc) {    86             x = -1; // Should be unreachable, really.    87         }    88     89         try {    90             x += 10 * testThrow(y);    91         } catch (MyException exc) {    92             x += 30;    93         } catch (MyOtherException exc) {    94             x += 20;    95         } catch (java.lang.Exception exc) {    96             x += -10; // Should be unreachable, really.    97         }    98         return x;    99     }   100    101     public static void main(String[] args) {   102         ExceptionTest test = new ExceptionTest();   103         try {   104             test.testThrow(0);   105             System.err.println("testThrow(0) failed!");   106         } catch (MyException exc) {   107             System.out.println("testThrow(0) correct: " + exc);   108         } catch (java.lang.Exception exc) {   109             System.err.println("testThrow(0) failed (MyException expected)!");   110         }   111         try {   112             test.testThrow(1);   113             System.err.println("testThrow(1) failed!");   114         } catch (MyOtherException exc) {   115             System.out.println("testThrow(1) correct: " + exc);   116         } catch (java.lang.Exception exc) {   117             System.err.println("testThrow(1) failed (MyOtherException expected)!");   118         }   119         try {   120             if (test.testThrow(2) != 1) {   121                 System.err.println("testThrow(2) failed!");   122             } else {   123                 System.out.println("testThrow(2) correct.");   124             }   125         } catch (java.lang.Exception exc) {   126             System.err.println("testThrow(2) failed (no exception expected)!");   127         }   128     }   129 }   130    131 class MyException extends java.lang.Exception {   132 }   133    134 class MyOtherException extends java.lang.Exception {   135 }   136    137 class MyUncheckedException extends java.lang.Exception {   138 }   139    140 // vim: tabstop=4 expandtab shiftwidth=4