Presentation is loading. Please wait.

Presentation is loading. Please wait.

© 2001 by Ashby M. Woolf Revision 2 Exceptions for Exceptional Circumstances Passing a Problem up the Chain of Command.

Similar presentations


Presentation on theme: "© 2001 by Ashby M. Woolf Revision 2 Exceptions for Exceptional Circumstances Passing a Problem up the Chain of Command."— Presentation transcript:

1 © 2001 by Ashby M. Woolf Revision 2 Exceptions for Exceptional Circumstances Passing a Problem up the Chain of Command

2 © 2001 by Ashby M. Woolf Revision 2 An Introduction Three Kinds of Exceptions

3 © 2001 by Ashby M. Woolf Revision 2 An Exception is Only an Unusual Circumstance The method expected it and knows how to deal with the circumstance –"Just Do it"."Just If It", "Just Case It", "Just The method expected the circumstance and cannot deal with it but thinks its caller may. –Now is when we throw an Exception Circumstance not expected and all we can do is crash and report the damage –Now is when we throw a RuntimeException

4 © 2001 by Ashby M. Woolf Revision 2 Passing the Buck meth1() { // a.meth2(); // } meth2() { // a.meth3(); // } meth3() { // // I expected this // but I have no // idea what to do? // } Got file name from user No such file! Got URL from webpage Can't find domain

5 © 2001 by Ashby M. Woolf Revision 2 Passing the Buck meth1() { // a.meth2(); // } meth2() { // a.meth3(); // } meth3() { // // I expected this // but I have no // idea what to do? // } meth1() { // er = a.meth2(); if(er == -4) { // Fix it } // } meth2() { // err = a.meth3(); if(err == -14){ return -4; } // } meth3() { // // I expected this // but I have no // idea what to do? return -14; // }

6 © 2001 by Ashby M. Woolf Revision 2 meth1() { // er = a.meth2(); if(er == -4) { // Fix it } // } meth2() { // err = a.meth3(); if(err == -14){ return -4; } // } meth3() { // // I expected this // but I have no // idea what to do? return -14; // } Passing the Buck meth1() { // try { a.meth2(); } catch(foo f){ // Fix it } // } meth2() throws Foo{ // a.meth3(); // } meth3() { // // I expected this // but I have no // idea what to do? throw new Foo; // } meth1() { // try { a.meth2(); } catch(foo f){ // Fix it } // } meth1() { // try { a.meth2(); } catch(Foo f){ // Fix it } // } meth3() throws Foo{ // // I expected this // but I have no // idea what to do? throw new Foo; // }

7 © 2001 by Ashby M. Woolf Revision 2 Passing the Buck meth1() { // try { a.meth2(); } catch(Foo f){ // Fix it } // } meth2() throws Foo{ // a.meth3(); // } meth3() throws Foo{ // // I expected this // but I have no // idea what to do? throw new Foo; // }

8 © 2001 by Ashby M. Woolf Revision 2 Throwable Error Exception A Part of the Exception Hierarchy IOException EOFException FileNotFoundException UnknownHostException 42 16 Foo

9 © 2001 by Ashby M. Woolf Revision 2 Passing the Buck meth1() { // try { a.meth2(); } catch(Foo f){ // Fix it } // } meth2() throws Foo{ // a.meth3(); // } meth3() throws Foo{ // // I expected this // but I have no // idea what to do? throw new Foo; // } meth3() throws Foo{ // // This was not // expected, crash // and report? throw new Foo; // } Abandoning Ship

10 © 2001 by Ashby M. Woolf Revision 2 Throwable Error ArithmeticException RuntimeException Exception A Part of the Exception Hierarchy ClassCastException NullPointerException IndexOutOfBoundsException IOException EOFException FileNotFoundException UnknownHostException 42 16 25 Foo Planned Crash and Burn Exception Tree! Foo

11 © 2001 by Ashby M. Woolf Revision 2 Passing the Buck meth1() { // try { a.meth2(); } catch(Foo f){ // Fix it } // } meth2() throws Foo{ // a.meth3(); // } meth1() { // try { a.meth2(); } catch(Foo f){ // Fix it } // } meth1() { // a.meth2(); // } meth2() throws Foo{ // a.meth3(); // } meth2() { // a.meth3(); // } meth3() throws Foo{ // // This was not // expected, crash // and report? throw new Foo; // } meth3() throws Foo{ // // This was not // expected, crash // and report? throw new Foo; // } meth3() { // // This was not // expected, crash // and report? throw new Foo; // } When will you want to throw a RuntimeException? Abandoning Ship Method or case not implemented yet.

12 © 2001 by Ashby M. Woolf Revision 2 Exception is Short for Unusual Circumstance The method expected it and knows how to deal with the circumstance –"Just Do it"."Just If It", "Just Case It", "Just The method expected the circumstance and cannot deal with it but thinks its caller may. –Now is when we throw an Exception Circumstance not expected and all we can do is crash and report the damage –Now is when we throw a RuntimeException The method expected it and knows how to deal with the circumstance –"Just Do it"."Just If It", "Just Case It", "Just The method expected the circumstance and cannot deal with it but thinks its caller may. –Now is when we throw an Exception Circumstance not expected and all we can do is crash and report the damage –Now is when we throw a RuntimeException The method expected it and knows how to deal with the circumstance –"Just Do it"."Just If It", "Just Case It", "Just The method expected the circumstance and cannot deal with it but thinks its caller may. –Now is when we throw an Exception Circumstance not expected and all we can do is crash and report the damage –Now is when we throw a RuntimeException

13 © 2001 by Ashby M. Woolf Revision 2 Passing the Buck meth1() { // try { a.meth2(); } catch(Foo f){ // Fix it } // } meth2() throws Foo{ // a.meth3(); // } meth3() throws Foo{ // // I expected this // but I have no // idea what to do? throw new Foo; // } What are these Exception objects? Try and catch syntax. Catching and rethrowing. Special Cases and Loose Ends

14 © 2001 by Ashby M. Woolf Revision 2 Try, Catch, and Finally void Meth1() { // Safe Code try { // Code which may throw exceptions meth2(); } catch(Type1Exception e) { // Code to handle Type1Exception } catch(Type2Exception e) { // Code to handle Type2Exception } catch(Type3Exception e) { // Code to handle Type3Exception } finally { // Executed no matter what! } // Safe Code }

15 © 2001 by Ashby M. Woolf Revision 2 Throwable Error Type7Exception Type1Exception Exception Catching a Hierarchy of Exceptions Type3Exception Type2Exception Type4Exception IOException EOFException FileNotFoundException UnknownHostException 42 16 catch(Type1Exception e) { // Handle Type1Exception } Type5Exception Type6Exception Type7Exception Type1Exception Type3Exception Type2Exception Type4Exception Type5Exception Type6Exception catch(Type2Exception e) { // Handle Type2Exception } Type7Exception Type3Exception Type2Exception Type4Exception Type5Exception Type6Exception catch(Type3Exception e) { // Handle Type2Exception } Type3Exception Type5Exception

16 © 2001 by Ashby M. Woolf Revision 2 The Catching Order Counts void Meth1() { // Safe Code try { // Code which may throw exceptions meth2(); } catch(Type1Exception e) { // Code to handle Type1Exception } catch(Type2Exception e) { // Code to handle Type2Exception } catch(Type3Exception e) { // Code to handle Type3Exception } finally { // Executed no matter what! } // Safe Code } void Meth1() { // Safe Code try { // Code which may throw exceptions meth2(); } catch(Type3Exception e) { // Code to handle Type1Exception } catch(Type2Exception e) { // Code to handle Type2Exception } catch(Type1Exception e) { // Code to handle Type3Exception } finally { // Executed no matter what! } // Safe Code } void Meth1() { // Safe Code try { // Code which may throw exceptions meth2(); } catch(Type3Exception e) { // Code to handle Type1Exception } catch(Type2Exception e) { // Code to handle Type2Exception } catch(Type1Exception e) { // Code to handle Type3Exception } finally { // Executed no matter what! } // Safe Code } void Meth1() { // Safe Code try { // Code which may throw exceptions meth2(); } catch(Type3Exception e) { // Code to handle Type1Exception } catch(Type2Exception e) { // Code to handle Type2Exception } catch(Type1Exception e) { // Code to handle Type3Exception } // Safe Code } finally {} Is optional. catch(Type1Exception e){} Would catch everything. Type7Exception Type1Exception Type3Exception Type2Exception Type4Exception Type5Exception Type6Exception Type7Exception Type1Exception Type3Exception Type2Exception Type4Exception Type5Exception Type6Exception Type7Exception Type1Exception Type3Exception Type2Exception Type4Exception Type5Exception Type6Exception Type7Exception Type1Exception Type3Exception Type2Exception Type4Exception Type5Exception Type6Exception

17 © 2001 by Ashby M. Woolf Revision 2 Building The Exception Tree class Type1Exception extends Exception {} class Type2Exception extends Type1Exception {} class Type3Exception extends Type2Exception {} class Type4Exception extends Type2Exception {} class Type5Exception extends Type3Exception {} class Type6Exception extends Type4Exception {} class Type7Exception extends Type4Exception {} Type7Exception Type1Exception Exception Type3Exception Type2Exception Type4Exception Type5Exception Type6Exception Type7Exception Type1Exception Type3Exception Type2Exception Type4Exception Type5Exception Type6Exception

18 © 2001 by Ashby M. Woolf Revision 2 Passing the Buck meth1() { // try { a.meth2(); } catch(Foo f){ // Fix it } // } meth2() throws Foo{ // a.meth3(); // } meth3() throws Foo{ // // I expected this // but I have no // idea what to do? throw new Foo; // }

19 © 2001 by Ashby M. Woolf Revision 2 meth1( ) public class TestException { void meth1() { try { System.out.println("In meth1() try block"); meth2(); } catch(Type3Exception e) { System.out.println("Caught A Type3"); } catch(Type2Exception e) { System.out.println("Caught A Type2"); } catch(Type1Exception e) { System.out.println("Caught A Type1"); } finally { System.out.println("In meth1() finally"); }

20 © 2001 by Ashby M. Woolf Revision 2 Passing the Buck meth1() { // try { a.meth2(); } catch(Foo f){ // Fix it } // } meth2() throws Foo{ // a.meth3(); // } meth3() throws Foo{ // // I expected this // but I have no // idea what to do? throw new Foo; // }

21 © 2001 by Ashby M. Woolf Revision 2 meth2( ) public class TestException { void meth2() throws Type1Exception { System.out.println("In meth2()"); meth3(); }

22 © 2001 by Ashby M. Woolf Revision 2 Passing the Buck meth1() { // try { a.meth2(); } catch(Foo f){ // Fix it } // } meth2() throws Foo{ // a.meth3(); // } meth3() throws Foo{ // // I expected this // but I have no // idea what to do? throw new Foo; // }

23 © 2001 by Ashby M. Woolf Revision 2 meth3( ) public class TestException { void meth3() throws Type1Exception { System.out.println("In meth3()"); if(false)throw new Type1Exception(); System.out.println("In meth3() after throw"); } Nothing thrown this time.

24 © 2001 by Ashby M. Woolf Revision 2 main( ) public class TestException { public static void main(String[] args) { TestException te = new TestException(); te.meth1(); } In meth1() try block In meth2() In meth3() In meth3() after throw In meth1() finally

25 © 2001 by Ashby M. Woolf Revision 2 Nothing Thrown public class TestException { void meth1() { try { System.out.println("In meth1() try block"); meth2(); } catch(Type3Exception e) { System.out.println("Caught A Type3"); } catch(Type2Exception e) { System.out.println("Caught A Type2"); } catch(Type1Exception e) { System.out.println("Caught A Type1"); } finally { System.out.println("In meth1() finally"); } Type7Exception Type1Exception Exception Type3Exception Type2Exception Type4Exception Type5Exception Type6Exception In meth1() try block In meth2() In meth3() In meth3() after throw In meth1() finally Nothing Thrown.

26 © 2001 by Ashby M. Woolf Revision 2 meth3( ) public class TestException { void meth3() throws Type1Exception { System.out.println("In meth3()"); if(false)throw new Type1Exception(); System.out.println("In meth3() after throw"); } public class TestException { void meth3() throws Type1Exception { System.out.println("In meth3()"); if(true)throw new Type1Exception(); System.out.println("In meth3() after throw"); }

27 © 2001 by Ashby M. Woolf Revision 2 Type1Exception Thrown public class TestException { void meth1() { try { System.out.println("In meth1() try block"); meth2(); } catch(Type3Exception e) { System.out.println("Caught A Type3"); } catch(Type2Exception e) { System.out.println("Caught A Type2"); } catch(Type1Exception e) { System.out.println("Caught A Type1"); } finally { System.out.println("In meth1() finally"); } Type7Exception Type1Exception Exception Type3Exception Type2Exception Type4Exception Type5Exception Type6Exception Type1Exception Type3Exception Type2Exception In meth1() try block In meth2() In meth3() Caught A Type1 In meth1() finally Type1Exception Thrown.

28 © 2001 by Ashby M. Woolf Revision 2 meth3( ) public class TestException { void meth3() throws Type1Exception { System.out.println("In meth3()"); if(true)throw new Type1Exception(); System.out.println("In meth3() after throw"); } public class TestException { void meth3() throws Type1Exception { System.out.println("In meth3()"); if(true)throw new Type2Exception(); System.out.println("In meth3() after throw"); }

29 © 2001 by Ashby M. Woolf Revision 2 Type2Exception Thrown public class TestException { void meth1() { try { System.out.println("In meth1() try block"); meth2(); } catch(Type3Exception e) { System.out.println("Caught A Type3"); } catch(Type2Exception e) { System.out.println("Caught A Type2"); } catch(Type1Exception e) { System.out.println("Caught A Type1"); } finally { System.out.println("In meth1() finally"); } Type7Exception Type1Exception Exception Type3Exception Type2Exception Type4Exception Type5Exception Type6Exception Type3Exception Type2Exception In meth1() try block In meth2() In meth3() Caught A Type2 In meth1() finally Type2Exception Thrown.

30 © 2001 by Ashby M. Woolf Revision 2 meth3( ) public class TestException { void meth3() throws Type1Exception { System.out.println("In meth3()"); if(true)throw new Type2Exception(); System.out.println("In meth3() after throw"); } public class TestException { void meth3() throws Type1Exception { System.out.println("In meth3()"); if(true)throw new Type3Exception(); System.out.println("In meth3() after throw"); }

31 © 2001 by Ashby M. Woolf Revision 2 Type3Exception Thrown public class TestException { void meth1() { try { System.out.println("In meth1() try block"); meth2(); } catch(Type3Exception e) { System.out.println("Caught A Type3"); } catch(Type2Exception e) { System.out.println("Caught A Type2"); } catch(Type1Exception e) { System.out.println("Caught A Type1"); } finally { System.out.println("In meth1() finally"); } Type7Exception Type1Exception Exception Type3Exception Type2Exception Type4Exception Type5Exception Type6Exception Type3Exception In meth1() try block In meth2() In meth3() Caught A Type3 In meth1() finally Type3Exception Thrown.

32 © 2001 by Ashby M. Woolf Revision 2 meth3( ) public class TestException { void meth3() throws Type1Exception { System.out.println("In meth3()"); if(true)throw new Type3Exception(); System.out.println("In meth3() after throw"); } public class TestException { void meth3() throws Type1Exception { System.out.println("In meth3()"); if(true)throw new Type5Exception(); System.out.println("In meth3() after throw"); }

33 © 2001 by Ashby M. Woolf Revision 2 Type5Exception Thrown public class TestException { void meth1() { try { System.out.println("In meth1() try block"); meth2(); } catch(Type3Exception e) { System.out.println("Caught A Type3"); } catch(Type2Exception e) { System.out.println("Caught A Type2"); } catch(Type1Exception e) { System.out.println("Caught A Type1"); } finally { System.out.println("In meth1() finally"); } Type7Exception Type1Exception Exception Type3Exception Type2Exception Type4Exception Type5Exception Type6Exception Type3Exception In meth1() try block In meth2() In meth3() Caught A Type3 In meth1() finally Type5Exception Thrown.

34 © 2001 by Ashby M. Woolf Revision 2 meth2( ) public class TestException { void meth2() throws Type1Exception { System.out.println("In meth2()"); meth3(); } public class TestException { void meth2() throws Type1Exception { System.out.println("In meth2()"); try { meth3(); } } public class TestException { void meth2() throws Type1Exception { System.out.println("In meth2()"); try { meth3(); } catch(Type1Exception e) { System.out.println("Caught an exception in meth2()"); throw e; }

35 © 2001 by Ashby M. Woolf Revision 2 Type5Exception Thrown public class TestException { void meth1() { try { System.out.println("In meth1() try block"); meth2(); } catch(Type3Exception e) { System.out.println("Caught A Type3"); } catch(Type2Exception e) { System.out.println("Caught A Type2"); } catch(Type1Exception e) { System.out.println("Caught A Type1"); } finally { System.out.println("In meth1() finally"); } Type7Exception Type1Exception Exception Type3Exception Type2Exception Type4Exception Type5Exception Type6Exception Type3Exception Type5Exception Thrown. In meth1() try block In meth2() In meth3() Caught an exception in meth2() Caught A Type3 In meth1() finally

36 © 2001 by Ashby M. Woolf Revision 2 public class TestException { void meth2() throws Type1Exception { System.out.println("In meth2()"); try { meth3(); } catch(Type1Exception e) { System.out.println("Caught an exception in meth2()"); throw e; } meth2( ) public class TestException { void meth2() throws Type1Exception { System.out.println("In meth2()"); meth3(); }

37 © 2001 by Ashby M. Woolf Revision 2 Throwable Error ArithmeticException RuntimeException Exception A Part of the Exception Hierarchy ClassCastException NullPointerException IndexOutOfBoundsException IOException EOFException FileNotFoundException UnknownHostException 42 16 25 Foo Throwable fillInStackTrace() String getLocalizedMessage() String getMessage() void printStackTrace() void printStackTrace(PrintStream s) void printStackTrace(PrintWriter s) String toString()

38 © 2001 by Ashby M. Woolf Revision 2 public class TestException { void meth1() { try { System.out.println("In meth1() try block"); meth2(); } catch(Type3Exception e) { System.out.println("Caught A Type3"); } catch(Type2Exception e) { System.out.println("Caught A Type2"); } catch(Type1Exception e) { System.out.println("Caught A Type1"); } finally { System.out.println("In meth1() finally"); } public class TestException { void meth1() { try { System.out.println("In meth1() try block"); meth2(); } catch(Type3Exception e) { System.out.println("Caught A Type3"); e.printStackTrace(System.out); } catch(Type2Exception e) { System.out.println("Caught A Type2"); } catch(Type1Exception e) { System.out.println("Caught A Type1"); } finally { System.out.println("In meth1() finally"); } public class TestException { void meth1() { try { System.out.println("In meth1() try block"); meth2(); } catch(Type3Exception e) { System.out.println("Caught A Type3"); e.printStackTrace(System.out); } catch(Type2Exception e) { System.out.println("Caught A Type2"); e.printStackTrace(System.out); } catch(Type1Exception e) { System.out.println("Caught A Type1"); e.printStackTrace(System.out); } finally { System.out.println("In meth1() finally"); } meth1( )

39 © 2001 by Ashby M. Woolf Revision 2 Type5Exception Thrown public class TestException { void meth1() { try { System.out.println("In meth1() try block"); meth2(); } catch(Type3Exception e) { System.out.println("Caught A Type3"); e.printStackTrace(System.out); } catch(Type2Exception e) { System.out.println("Caught A Type2"); e.printStackTrace(System.out); } catch(Type1Exception e) { System.out.println("Caught A Type1"); e.printStackTrace(System.out); } finally { System.out.println("In meth1() finally"); } Type7Exception Type1Exception Exception Type3Exception Type2Exception Type4Exception Type5Exception Type6Exception Type3Exception Type5Exception Thrown. In meth1() try block In meth2() In meth3() Caught A Type3 Type5Exception at TestException.meth3(TestException.java:40) at TestException.meth2(TestException.java:36) at TestException.meth1(TestException.java:13) at TestException.main(TestException.java:46) In meth1() finally

40 © 2001 by Ashby M. Woolf Revision 2 public class TestException { void meth1() { try { System.out.println("In meth1() try block"); meth2(); } catch(Type3Exception e) { System.out.println("Caught A Type3"); e.printStackTrace(System.out); } catch(Type2Exception e) { System.out.println("Caught A Type2"); e.printStackTrace(System.out); } catch(Type1Exception e) { System.out.println("Caught A Type1"); e.printStackTrace(System.out); } finally { System.out.println("In meth1() finally"); } public class TestException { void meth1() { try { System.out.println("In meth1() try block"); meth2(); } catch(Type3Exception e) { System.out.println("Caught A Type3"); e.printStackTrace(System.out); System.out.println("toString() = " + e); } catch(Type2Exception e) { System.out.println("Caught A Type2"); e.printStackTrace(System.out); } catch(Type1Exception e) { System.out.println("Caught A Type1"); e.printStackTrace(System.out); } finally { System.out.println("In meth1() finally"); } public class TestException { void meth1() { try { System.out.println("In meth1() try block"); meth2(); } catch(Type3Exception e) { System.out.println("Caught A Type3"); e.printStackTrace(System.out); System.out.println("toString() = " + e); } catch(Type2Exception e) { System.out.println("Caught A Type2"); e.printStackTrace(System.out); System.out.println("toString() = " + e); } catch(Type1Exception e) { System.out.println("Caught A Type1"); e.printStackTrace(System.out); System.out.println("toString() = " + e); } finally { System.out.println("In meth1() finally"); } meth1( )

41 © 2001 by Ashby M. Woolf Revision 2 Type5Exception Thrown public class TestException { void meth1() { try { System.out.println("In meth1() try block"); meth2(); } catch(Type3Exception e) { System.out.println("Caught A Type3"); e.printStackTrace(System.out); System.out.println("toString() = " + e); } catch(Type2Exception e) { System.out.println("Caught A Type2"); e.printStackTrace(System.out); System.out.println("toString() = " + e); } catch(Type1Exception e) { System.out.println("Caught A Type1"); e.printStackTrace(System.out); System.out.println("toString() = " + e); } finally { System.out.println("In meth1() finally"); } Type7Exception Type1Exception Exception Type3Exception Type2Exception Type4Exception Type5Exception Type6Exception Type3Exception Type5Exception Thrown. In meth1() try block In meth2() In meth3() Caught A Type3 Type5Exception at TestException.meth3(TestException.java:40) at TestException.meth2(TestException.java:36) at TestException.meth1(TestException.java:13) at TestException.main(TestException.java:46) toString() = Type5Exception In meth1() finally

42 © 2001 by Ashby M. Woolf Revision 2 Adding Constructors to Our Exceptions class Type1Exception extends Exception {} class Type2Exception extends Type1Exception {} class Type3Exception extends Type2Exception {} class Type4Exception extends Type2Exception {} class Type5Exception extends Type3Exception {} class Type6Exception extends Type4Exception {} class Type7Exception extends Type4Exception {} class Type1Exception extends Exception { } class Type2Exception extends Type1Exception { } class Type1Exception extends Exception { Type1Exception(String s) { super(s); } class Type2Exception extends Type1Exception { Type2Exception(String s) { super(s); } class Type1Exception extends Exception { Type1Exception(String s) { super(s); } Type1Exception() { super(); } class Type2Exception extends Type1Exception { Type2Exception(String s) { super(s); } Type2Exception() { super(); }

43 © 2001 by Ashby M. Woolf Revision 2 meth3( ) public class TestException { void meth3() throws Type1Exception { System.out.println("In meth3()"); if(true)throw new Type5Exception(); System.out.println("In meth3() after throw"); } public class TestException { void meth3() throws Type1Exception { System.out.println("In meth3()"); if(true)throw new Type5Exception("I am outta here!"); System.out.println("In meth3() after throw"); }

44 © 2001 by Ashby M. Woolf Revision 2 public class TestException { void meth1() { try { System.out.println("In meth1() try block"); meth2(); } catch(Type3Exception e) { System.out.println("Caught A Type3"); e.printStackTrace(System.out); System.out.println("toString() = " + e); } catch(Type2Exception e) { System.out.println("Caught A Type2"); e.printStackTrace(System.out); System.out.println("toString() = " + e); } catch(Type1Exception e) { System.out.println("Caught A Type1"); e.printStackTrace(System.out); System.out.println("toString() = " + e); } finally { System.out.println("In meth1() finally"); } public class TestException { void meth1() { try { System.out.println("In meth1() try block"); meth2(); } catch(Type3Exception e) { System.out.println("Caught A Type3"); e.printStackTrace(System.out); System.out.println("toString() = " + e); System.out.println("Message = " + e.getMessage()); } catch(Type2Exception e) { System.out.println("Caught A Type2"); e.printStackTrace(System.out); System.out.println("toString() = " + e); System.out.println("Message = " + e.getMessage()); } catch(Type1Exception e) { System.out.println("Caught A Type1"); e.printStackTrace(System.out); System.out.println("toString() = " + e); System.out.println("Message = " + e.getMessage()); } finally { System.out.println("In meth1() finally"); } meth1( )

45 © 2001 by Ashby M. Woolf Revision 2 Type5Exception Thrown public class TestException { void meth1() { try { System.out.println("In meth1() try block"); meth2(); } catch(Type3Exception e) { System.out.println("Caught A Type3"); e.printStackTrace(System.out); System.out.println("toString() = " + e); System.out.println("Message = " + e.getMessage()); } catch(Type2Exception e) { System.out.println("Caught A Type2"); e.printStackTrace(System.out); System.out.println("toString() = " + e); System.out.println("Message = " + e.getMessage()); } catch(Type1Exception e) { System.out.println("Caught A Type1"); e.printStackTrace(System.out); System.out.println("toString() = " + e); System.out.println("Message = " + e.getMessage()); } finally { System.out.println("In meth1() finally"); } Type7Exception Type1Exception Exception Type3Exception Type2Exception Type4Exception Type5Exception Type6Exception Type3Exception Type5Exception Thrown. In meth1() try block In meth2() In meth3() Caught A Type3 Type5Exception: I am outta here. at TestException.meth3(TestException.java:92) at TestException.meth2(TestException.java:88) at TestException.meth1(TestException.java:62) at TestException.main(TestException.java:98) toString() = Type5Exception: I am outta here. Message = I am outta here. In meth1() finally

46 © 2001 by Ashby M. Woolf Revision 2 Restrictions When Overriding class Base { void meth() throws Type4Exception { // meth body } class Derived extends Base { void meth() throws Type?Exception { // meth body } Type7Exception Type1Exception Exception Type3Exception Type2Exception Type4Exception Type5Exception Type6Exception Type7Exception Type4Exception Type6Exception Type1Exception Exception Type3Exception Type2Exception Type5Exception Which Exceptions are Allowed? Overrides meth( ) in Base

47 © 2001 by Ashby M. Woolf Revision 2 Exceptions Plan A Use Existing Hierarchy of Exceptions Catch Individually or by Groups in the Hierarchy Use Finally for Cleanup to Insure it Gets Done

48 © 2001 by Ashby M. Woolf Revision 2 Exceptions Plan B Create Logical Hierarchy of Exceptions Give Them Long Descriptive Names

49 © 2001 by Ashby M. Woolf Revision 2 Exceptions Plan C Create Logical Hierarchy of Exceptions Give Them Long Descriptive Names Use the Constructor to Add a Message Remember you can always add members to your Exceptions

50 © 2001 by Ashby M. Woolf Revision 2 Summary Expected circumstance and cannot deal with it but thinks its caller may. –Use the Existing Exception Hierarchy –Make Your Own Hierarchy –Make Your Own Hierarchy and Add Function Circumstance not expected and all we can do is crash and report the damage –Throw a RuntimeException

51 © 2001 by Ashby M. Woolf Revision 2 End of Content


Download ppt "© 2001 by Ashby M. Woolf Revision 2 Exceptions for Exceptional Circumstances Passing a Problem up the Chain of Command."

Similar presentations


Ads by Google