paul@16 | 1 | public class ExceptionTest { |
paul@16 | 2 | |
paul@20 | 3 | public int testThrow(int x) throws java.lang.Exception { |
paul@20 | 4 | if (x == 0) { |
paul@16 | 5 | throw new MyException(); |
paul@20 | 6 | } else if (x == 1) { |
paul@20 | 7 | throw new MyOtherException(); |
paul@20 | 8 | } else { |
paul@20 | 9 | return 1; |
paul@20 | 10 | } |
paul@20 | 11 | } |
paul@20 | 12 | |
paul@20 | 13 | public int testCatch(int x) { |
paul@20 | 14 | try { |
paul@20 | 15 | if (x == 0) { |
paul@20 | 16 | throw new MyException(); |
paul@20 | 17 | } else if (x == 1) { |
paul@20 | 18 | throw new MyOtherException(); |
paul@20 | 19 | } else { |
paul@20 | 20 | x = 1; |
paul@20 | 21 | } |
paul@16 | 22 | } catch (MyException exc) { |
paul@20 | 23 | x = 3; |
paul@20 | 24 | } catch (MyOtherException exc) { |
paul@20 | 25 | x = 2; |
paul@20 | 26 | } |
paul@20 | 27 | return x; |
paul@20 | 28 | } |
paul@20 | 29 | |
paul@20 | 30 | public int testIncomingCatch(int x) { |
paul@20 | 31 | try { |
paul@20 | 32 | return testThrow(x); |
paul@20 | 33 | } catch (MyException exc) { |
paul@20 | 34 | return 3; |
paul@20 | 35 | } catch (MyOtherException exc) { |
paul@20 | 36 | return 2; |
paul@20 | 37 | } catch (java.lang.Exception exc) { |
paul@20 | 38 | return -1; // Should be unreachable, really. |
paul@16 | 39 | } |
paul@16 | 40 | } |
paul@16 | 41 | |
paul@19 | 42 | public int testFinally(int x) throws java.lang.Exception { |
paul@19 | 43 | try { |
paul@19 | 44 | if (x == 0) { |
paul@20 | 45 | x = 3; |
paul@19 | 46 | throw new MyException(); |
paul@19 | 47 | } else if (x == 1) { |
paul@20 | 48 | x = 2; |
paul@19 | 49 | throw new MyOtherException(); |
paul@19 | 50 | } |
paul@25 | 51 | x = 1; |
paul@19 | 52 | } finally { |
paul@25 | 53 | x += 10; |
paul@19 | 54 | } |
paul@19 | 55 | return x; |
paul@19 | 56 | } |
paul@19 | 57 | |
paul@27 | 58 | public int testCatchFinally(int x) throws MyUncheckedException { |
paul@16 | 59 | try { |
paul@16 | 60 | if (x == 0) { |
paul@16 | 61 | throw new MyException(); |
paul@16 | 62 | } else if (x == 1) { |
paul@16 | 63 | throw new MyOtherException(); |
paul@27 | 64 | } else if (x == 2) { |
paul@27 | 65 | throw new MyUncheckedException(); |
paul@16 | 66 | } |
paul@25 | 67 | x = 1; |
paul@16 | 68 | } catch (MyException exc) { |
paul@16 | 69 | x = 3; |
paul@16 | 70 | } catch (MyOtherException exc) { |
paul@16 | 71 | x = 2; |
paul@16 | 72 | } finally { |
paul@25 | 73 | x += 10; |
paul@16 | 74 | } |
paul@16 | 75 | return x; |
paul@16 | 76 | } |
paul@27 | 77 | |
paul@27 | 78 | public int testMultipleCatch(int x, int y) { |
paul@27 | 79 | try { |
paul@27 | 80 | x = testThrow(x); |
paul@27 | 81 | } catch (MyException exc) { |
paul@27 | 82 | x = 3; |
paul@27 | 83 | } catch (MyOtherException exc) { |
paul@27 | 84 | x = 2; |
paul@27 | 85 | } catch (java.lang.Exception exc) { |
paul@27 | 86 | x = -1; // Should be unreachable, really. |
paul@27 | 87 | } |
paul@27 | 88 | |
paul@27 | 89 | try { |
paul@27 | 90 | x += 10 * testThrow(y); |
paul@27 | 91 | } catch (MyException exc) { |
paul@27 | 92 | x += 30; |
paul@27 | 93 | } catch (MyOtherException exc) { |
paul@27 | 94 | x += 20; |
paul@27 | 95 | } catch (java.lang.Exception exc) { |
paul@27 | 96 | x += -10; // Should be unreachable, really. |
paul@27 | 97 | } |
paul@27 | 98 | return x; |
paul@27 | 99 | } |
paul@16 | 100 | } |
paul@16 | 101 | |
paul@16 | 102 | class MyException extends java.lang.Exception { |
paul@16 | 103 | } |
paul@16 | 104 | |
paul@16 | 105 | class MyOtherException extends java.lang.Exception { |
paul@16 | 106 | } |
paul@16 | 107 | |
paul@27 | 108 | class MyUncheckedException extends java.lang.Exception { |
paul@27 | 109 | } |
paul@27 | 110 | |
paul@16 | 111 | // vim: tabstop=4 expandtab shiftwidth=4 |