510 likes | 522 Views
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.
E N D
Exceptions for Exceptional Circumstances Passing a Problem up the Chain of Command
An Introduction Three Kinds of Exceptions
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
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
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; // ••• }
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; // ••• }
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; // ••• }
A Part of the Exception Hierarchy Throwable Exception Error 42 IOException 16 Foo FileNotFoundException EOFException UnknownHostException
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; // ••• }
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
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.
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
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?
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 }
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
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.
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
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; // ••• }
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( )
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; // ••• }
meth2( ) public class TestException { • • • void meth2() throws Type1Exception { System.out.println("In meth2()"); meth3(); } • • •
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( ) 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.
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
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
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"); } • • •
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
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"); } • • •
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
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"); } • • •
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
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"); } • • •
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
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; } } • • •
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
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(); } • • •
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
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( )
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
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( )
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
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(); } } • • •
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"); } • • •
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( )
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
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?
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
Exceptions Plan B • Create Logical Hierarchy of Exceptions • Give Them Long Descriptive Names
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
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