1 / 51

Exceptions for Exceptional Circumstances

Learn the three kinds of exceptions - expected, unexpected, and unmanaged - and how to handle them effectively in your code. Explore the Exception Hierarchy and when to throw a RuntimeException.

pettway
Download Presentation

Exceptions for Exceptional Circumstances

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Exceptions for Exceptional Circumstances Passing a Problem up the Chain of Command

  2. An Introduction Three Kinds of Exceptions

  3. An Exception is Only anUnusual 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. Passing the Buck meth1() { // ••• a.meth2(); // ••• } Got file name from user meth2() { // ••• a.meth3(); // ••• } No such file! Got URL from webpage meth3() { // ••• // I expected this // but I have no // idea what to do? // ••• } Can't find domain

  5. meth1() { // ••• a.meth2(); // ••• } meth2() { // ••• a.meth3(); // ••• } meth3() { // ••• // I expected this // but I have no // idea what to do? // ••• } Passing the Buck 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. 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 } // ••• } meth1() { // ••• try { a.meth2(); } catch(foo f){ // Fix it } // ••• } meth1() { // ••• try { a.meth2(); } catch(Foo f){ // Fix it } // ••• } meth2() throwsFoo{ // ••• a.meth3(); // ••• } meth3() { // ••• // I expected this // but I have no // idea what to do? throw new Foo; // ••• } meth3() throws Foo{ // ••• // I expected this // but I have no // idea what to do? throw new Foo; // ••• }

  7. 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. A Part of the Exception Hierarchy Throwable Exception Error 42 IOException 16 Foo FileNotFoundException EOFException UnknownHostException

  9. Abandoning Ship 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; // ••• }

  10. A Part of the Exception Hierarchy Throwable Planned Crash and Burn Exception Tree! Exception Error 42 RuntimeException IOException 25 16 Foo Foo NullPointerException FileNotFoundException EOFException ClassCastException UnknownHostException IndexOutOfBoundsException ArithmeticException

  11. Abandoning Ship Passing the Buck meth1() { // ••• try { a.meth2(); } catch(Foo f){ // Fix it } // ••• } meth1() { // ••• try { a.meth2(); } catch(Foo f){ // Fix it } // ••• } meth1() { // ••• a.meth2(); // ••• } When will you want to throw a RuntimeException? meth2() throws Foo{ // ••• a.meth3(); // ••• } 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; // ••• } Method or case not implemented yet.

  12. 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. Passing the Buck meth1() { // ••• try { a.meth2(); } catch(Foo f){ // Fix it } // ••• } Catching and rethrowing. Special Cases and Loose Ends meth2() throws Foo{ // ••• a.meth3(); // ••• } Try and catch syntax. meth3() throws Foo{ // ••• // I expected this // but I have no // idea what to do? throw new Foo; // ••• } What are these Exception objects?

  14. 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. Catching a Hierarchy of Exceptions Throwable Exception Error 42 Type1Exception Type1Exception catch(Type1Exception e) { // Handle Type1Exception } IOException Type2Exception Type2Exception Type2Exception 16 Type4Exception Type4Exception Type4Exception catch(Type2Exception e) { // Handle Type2Exception } FileNotFoundException Type3Exception Type3Exception Type3Exception Type3Exception EOFException UnknownHostException catch(Type3Exception e) { // Handle Type2Exception } Type5Exception Type5Exception Type5Exception Type5Exception Type7Exception Type7Exception Type7Exception Type6Exception Type6Exception Type6Exception

  16. Type1Exception Type1Exception Type2Exception Type2Exception Type3Exception Type3Exception Type4Exception Type4Exception Type5Exception Type5Exception Type7Exception Type7Exception Type1Exception Type6Exception Type6Exception Type1Exception Type2Exception Type2Exception Type3Exception Type4Exception Type3Exception Type4Exception Type5Exception Type7Exception Type5Exception Type6Exception Type7Exception Type6Exception The Catching Order Counts 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 } 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(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 } catch(Type1Exception e){} Would catch everything. finally {} Is optional.

  17. Building The Exception Tree Exception 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 {} Type1Exception Type1Exception Type2Exception Type2Exception Type4Exception Type4Exception Type3Exception Type3Exception Type5Exception Type5Exception Type7Exception Type7Exception Type6Exception Type6Exception

  18. 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. 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"); } } meth1( )

  20. 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. meth2( ) public class TestException { • • • void meth2() throws Type1Exception { System.out.println("In meth2()"); meth3(); } • • •

  22. 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. 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. 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. 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"); } } Nothing Thrown Exception Type1Exception Nothing Thrown. Type2Exception In meth1() try block In meth2() In meth3() In meth3() after throw In meth1() finally Type4Exception Type3Exception Type5Exception Type7Exception Type6Exception

  26. 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. 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"); } } Type1Exception Thrown Exception Type1Exception Thrown. Type1Exception Type1Exception Type2Exception Type2Exception Type4Exception Type3Exception Type3Exception Type5Exception In meth1() try block In meth2() In meth3() Caught A Type1 In meth1() finally Type7Exception Type6Exception

  28. 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. 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"); } } Type2Exception Thrown Exception Type1Exception Type2Exception Thrown. Type2Exception Type2Exception Type4Exception Type3Exception Type3Exception Type5Exception In meth1() try block In meth2() In meth3() Caught A Type2 In meth1() finally Type7Exception Type6Exception

  30. 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 Type2Exception(); System.out.println("In meth3() after throw"); } • • •

  31. 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"); } } Type3Exception Thrown Exception Type1Exception Type2Exception Type3Exception Thrown. Type4Exception Type3Exception Type3Exception Type5Exception In meth1() try block In meth2() In meth3() Caught A Type3 In meth1() finally Type7Exception Type6Exception

  32. 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. 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"); } } Type5Exception Thrown Exception Type1Exception Type2Exception Type4Exception Type3Exception Type3Exception Type5Exception Thrown. Type5Exception In meth1() try block In meth2() In meth3() Caught A Type3 In meth1() finally Type7Exception Type6Exception

  34. 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. 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"); } } Type5Exception Thrown Exception Type1Exception Type2Exception Type4Exception Type3Exception Type3Exception Type5Exception Thrown. Type5Exception In meth1() try block In meth2() In meth3() Caught an exception in meth2() Caught A Type3 In meth1() finally Type7Exception Type6Exception

  36. meth2( ) 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; } } • • • public class TestException { • • • void meth2() throws Type1Exception { System.out.println("In meth2()"); meth3(); } • • •

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

  38. 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. 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"); } } Type5Exception Thrown Exception Type1Exception Type2Exception 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 Type4Exception Type3Exception Type3Exception Type5Exception Thrown. Type5Exception Type7Exception Type6Exception

  40. 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. 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"); } } Type5Exception Thrown Exception Type1Exception Type2Exception 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 Type4Exception Type3Exception Type3Exception Type5Exception Thrown. Type5Exception Type7Exception Type6Exception

  42. 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. 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. 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. 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"); } } Type5Exception Thrown Exception Type1Exception Type2Exception 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 Type4Exception Type3Exception Type3Exception Type5Exception Thrown. Type5Exception Type7Exception Type6Exception

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

  47. 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. Exceptions Plan B • Create Logical Hierarchy of Exceptions • Give Them Long Descriptive Names

  49. 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. 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

More Related