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