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 102 class MyException extends java.lang.Exception { 103 } 104 105 class MyOtherException extends java.lang.Exception { 106 } 107 108 class MyUncheckedException extends java.lang.Exception { 109 } 110 111 // vim: tabstop=4 expandtab shiftwidth=4