javaclass

tests/ExceptionTest.java

177:9523263f1ec1
2005-02-21 Paul Boddie Attempted to add more sophisticated circular import handling.
     1 public class ExceptionTest {     2     public int last;     3      4     public int testThrow(int x) throws java.lang.Exception {     5         if (x == 0) {     6             throw new MyException();     7         } else if (x == 1) {     8             throw new MyOtherException();     9         } else {    10             return 1;    11         }    12     }    13     14     public int testCatch(int x) {    15         try {    16             if (x == 0) {    17                 throw new MyException();    18             } else if (x == 1) {    19                 throw new MyOtherException();    20             } else {    21                 x = 1;    22             }    23         } catch (MyException exc) {    24             x = 3;    25         } catch (MyOtherException exc) {    26             x = 2;    27         }    28         return x;    29     }    30     31     public int testIncomingCatch(int x) {    32         try {    33             return testThrow(x);    34         } catch (MyException exc) {    35             return 3;    36         } catch (MyOtherException exc) {    37             return 2;    38         } catch (java.lang.Exception exc) {    39             return -1; // Should be unreachable, really.    40         }    41     }    42     43     public int testFinally(int x) throws java.lang.Exception {    44         try {    45             if (x == 0) {    46                 x = 3;    47                 throw new MyException();    48             } else if (x == 1) {    49                 x = 2;    50                 throw new MyOtherException();    51             }    52             x = 1;    53         } finally {    54             x += 10;    55             last = x;    56         }    57         return x;    58     }    59     60     public int testCatchFinally(int x) throws MyUncheckedException {    61         try {    62             if (x == 0) {    63                 throw new MyException();    64             } else if (x == 1) {    65                 throw new MyOtherException();    66             } else if (x == 2) {    67                 throw new MyUncheckedException();    68             }    69             x = 1;    70         } catch (MyException exc) {    71             x = 3;    72         } catch (MyOtherException exc) {    73             x = 2;    74         } finally {    75             x += 10;    76             last = x;    77         }    78         return x;    79     }    80     81     public int testMultipleCatch(int x, int y) {    82         try {    83             x = testThrow(x);    84         } catch (MyException exc) {    85             x = 3;    86         } catch (MyOtherException exc) {    87             x = 2;    88         } catch (java.lang.Exception exc) {    89             x = -1; // Should be unreachable, really.    90         }    91     92         try {    93             x += 10 * testThrow(y);    94         } catch (MyException exc) {    95             x += 30;    96         } catch (MyOtherException exc) {    97             x += 20;    98         } catch (java.lang.Exception exc) {    99             x += -10; // Should be unreachable, really.   100         }   101         return x;   102     }   103    104     public static void main(String[] args) {   105         ExceptionTest test = new ExceptionTest();   106         try {   107             test.testThrow(0);   108             System.err.println("testThrow(0) failed!");   109         } catch (MyException exc) {   110             System.out.println("testThrow(0) correct: " + exc);   111         } catch (java.lang.Exception exc) {   112             System.err.println("testThrow(0) failed (MyException expected)!");   113         }   114         try {   115             test.testThrow(1);   116             System.err.println("testThrow(1) failed!");   117         } catch (MyOtherException exc) {   118             System.out.println("testThrow(1) correct: " + exc);   119         } catch (java.lang.Exception exc) {   120             System.err.println("testThrow(1) failed (MyOtherException expected)!");   121         }   122         try {   123             if (test.testThrow(2) != 1) {   124                 System.err.println("testThrow(2) failed!");   125             } else {   126                 System.out.println("testThrow(2) correct.");   127             }   128         } catch (java.lang.Exception exc) {   129             System.err.println("testThrow(2) failed (no exception expected)!");   130         }   131    132         if (test.testCatch(0) != 3) {   133             System.err.println("testCatch(0) failed!");   134         } else {   135             System.out.println("testCatch(0) correct.");   136         }   137         if (test.testCatch(1) != 2) {   138             System.err.println("testCatch(1) failed!");   139         } else {   140             System.out.println("testCatch(1) correct.");   141         }   142         if (test.testCatch(2) != 1) {   143             System.err.println("testCatch(2) failed!");   144         } else {   145             System.out.println("testCatch(2) correct.");   146         }   147    148         if (test.testIncomingCatch(0) != 3) {   149             System.err.println("testIncomingCatch(0) failed!");   150         } else {   151             System.out.println("testIncomingCatch(0) correct.");   152         }   153         if (test.testIncomingCatch(1) != 2) {   154             System.err.println("testIncomingCatch(1) failed!");   155         } else {   156             System.out.println("testIncomingCatch(1) correct.");   157         }   158         if (test.testIncomingCatch(2) != 1) {   159             System.err.println("testIncomingCatch(2) failed!");   160         } else {   161             System.out.println("testIncomingCatch(2) correct.");   162         }   163    164         try {   165             test.testFinally(0);   166             System.err.println("testFinally(0) failed!");   167         } catch (MyException exc) {   168             if (test.last == 13) {   169                 System.out.println("testFinally(0) correct: set " + test.last);   170             } else {   171                 System.err.println("testFinally(0) failed: set " + test.last);   172             }   173         } catch (java.lang.Exception exc) {   174             System.err.println("testFinally(0) failed!");   175         }   176         try {   177             test.testFinally(1);   178             System.err.println("testFinally(1) failed!");   179         } catch (MyOtherException exc) {   180             if (test.last == 12) {   181                 System.out.println("testFinally(1) correct: set " + test.last);   182             } else {   183                 System.err.println("testFinally(1) failed: set " + test.last);   184             }   185         } catch (java.lang.Exception exc) {   186             System.err.println("testFinally(1) failed!");   187         }   188         try {   189             if (test.testFinally(2) != 11) {   190                 System.err.println("testFinally(2) failed!");   191             } else {   192                 System.out.println("testFinally(2) correct.");   193             }   194         } catch (java.lang.Exception exc) {   195             System.err.println("testFinally(2) failed!");   196         }   197    198         try {   199             if (test.testCatchFinally(0) != 13) {   200                 System.err.println("testCatchFinally(0) failed!");   201             } else {   202                 System.out.println("testCatchFinally(0) correct.");   203             }   204         } catch (MyUncheckedException exc) {   205             System.err.println("testCatchFinally(0) failed!");   206         }   207         try {   208             if (test.testCatchFinally(1) != 12) {   209                 System.err.println("testCatchFinally(1) failed!");   210             } else {   211                 System.out.println("testCatchFinally(1) correct.");   212             }   213         } catch (MyUncheckedException exc) {   214             System.err.println("testCatchFinally(0) failed!");   215         }   216         try {   217             test.testCatchFinally(2);   218             System.err.println("testCatchFinally(2) failed!");   219         } catch (MyUncheckedException exc) {   220             if (test.last != 12) {   221                 System.err.println("testCatchFinally(2) failed!");   222             } else {   223                 System.out.println("testCatchFinally(2) correct.");   224             }   225         }   226    227         if (test.testMultipleCatch(0, 1) != 23) {   228             System.err.println("testMultipleCatch(0, 1) failed!");   229         } else {   230             System.out.println("testMultipleCatch(0, 1) correct.");   231         }   232         if (test.testMultipleCatch(1, 2) != 12) {   233             System.err.println("testMultipleCatch(1, 2) failed!");   234         } else {   235             System.out.println("testMultipleCatch(1, 2) correct.");   236         }   237         if (test.testMultipleCatch(2, 0) != 31) {   238             System.err.println("testMultipleCatch(2, 0) failed!");   239         } else {   240             System.out.println("testMultipleCatch(2, 0) correct.");   241         }   242     }   243 }   244    245 class MyException extends java.lang.Exception {   246 }   247    248 class MyOtherException extends java.lang.Exception {   249 }   250    251 class MyUncheckedException extends java.lang.Exception {   252 }   253    254 // vim: tabstop=4 expandtab shiftwidth=4