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@19 | 51 | } finally { |
paul@19 | 52 | x = 1; |
paul@19 | 53 | } |
paul@19 | 54 | return x; |
paul@19 | 55 | } |
paul@19 | 56 | |
paul@19 | 57 | public int testCatchFinally(int x) { |
paul@16 | 58 | try { |
paul@16 | 59 | if (x == 0) { |
paul@16 | 60 | throw new MyException(); |
paul@16 | 61 | } else if (x == 1) { |
paul@16 | 62 | throw new MyOtherException(); |
paul@16 | 63 | } |
paul@16 | 64 | } catch (MyException exc) { |
paul@16 | 65 | x = 3; |
paul@16 | 66 | } catch (MyOtherException exc) { |
paul@16 | 67 | x = 2; |
paul@16 | 68 | } finally { |
paul@16 | 69 | x = 1; |
paul@16 | 70 | } |
paul@16 | 71 | return x; |
paul@16 | 72 | } |
paul@16 | 73 | } |
paul@16 | 74 | |
paul@16 | 75 | class MyException extends java.lang.Exception { |
paul@16 | 76 | } |
paul@16 | 77 | |
paul@16 | 78 | class MyOtherException extends java.lang.Exception { |
paul@16 | 79 | } |
paul@16 | 80 | |
paul@16 | 81 | // vim: tabstop=4 expandtab shiftwidth=4 |