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

Slides:



Advertisements
Similar presentations
Chapter 17 Failures and exceptions. This chapter discusses n Failure. n The meaning of system failure. n Causes of failure. n Handling failure. n Exception.
Advertisements

Exceptions CSE301 University of Sunderland Harry Erwin, PhD.
Exceptions: when things go wrong. Various sources of error public static doSomething() { int i = 3.0; while(!done); { int i = false } ) Syntactic errors.
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
COMP 121 Week 5: Exceptions and Exception Handling.
Exceptions and Exception Handling Carl Alphonce CSE116 March 9, 2007.
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions.
MIT-AITI Lecture 14: Exceptions Handling Errors with Exceptions Kenya 2005.
For use of Cleveland State's IST410 Students only 1 Exception.
EXCEPTIONS. What’s an exception?? Change the flow of control when something important happens ideally - we catch errors at compile time doesn’t happen.
Java Exceptions. Types of exceptions  Checked exceptions: A checked exception is an exception that is typically a user error or a problem that cannot.
MSc IT Programming Methodology (2). THROWS an EXCEPTION Errors?
Lecture 27 Exceptions COMP1681 / SE15 Introduction to Programming.
Objectives Understanding what an exception is Understanding the heirarchy of exception classes Learn the types of exception and how to catch and handle.
Exception Handling.  What are errors?  What does exception handling allow us to do?  Where are exceptions handled?  What does exception handling facilitate?
Lecture 28 More on Exceptions COMP1681 / SE15 Introduction to Programming.
Exceptions and Exception Handling (2) Carl Alphonce CSE116.
Exceptions and Exception Handling (continued) Carl Alphonce CSE116.
11-Jun-15 Exceptions. 2 Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a.
1 From Yesterday private = accessible only to the class that declares it public = accessible to any class at all protected = accessible to the class and.
Exceptions Used to signal errors or unexpected situations to calling code Should not be used for problems that can be dealt with reasonably within local.
交通大學資訊工程學系 Programming in Java Exception Handling again 蔡文能 交通大學資訊工程學系
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
交通大學資訊工程學系 Programming in Java Exception Handling 蔡文能 交通大學資訊工程學系
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
06 - Exceptions. 2 ©S. Uchitel, 2004 A familiar sight? Bluescreen.scr.
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
Exception Handling. Exceptions and Errors When a problem encounters and unexpected termination or fault, it is called an exception When we try and divide.
Chapter 13 Exception Handling F Claiming Exceptions F Throwing Exceptions F Catching Exceptions F Rethrowing Exceptions  The finally Clause F Cautions.
Exception: Java Error Handling Yingcai Xiao. Error Handling In-line if(error) System.out.println(“Error occurred here.”); Block-based if(error) { ErrorCode.
06 Exception Handling. 2 Contents What is an Exception? Exception-handling in Java Types of Exceptions Exception Hierarchy try-catch()-finally Statement.
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
Slides Credit Umair Javed LUMS Web Application Development.
Java Programming: Guided Learning with Early Objects
Exception Handling Part 2: Creating and Throwing Exceptions CSIS 3701: Advanced Object Oriented Programming.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
Exceptions By the end of this lecture you should be able to: explain the term exception; distinguish between checked and unchecked exception classes in.
1 Exception handling in Java Reading for this lecture: Weiss, Section 2.5 (exception handling), p. 47. ProgramLive, chapter 10. I need to know whether.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Exceptions Handling Exceptions with try and catch The finally-block The throws.
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
Exceptions Handling Prepared by: Ligemm Mae del Castillo.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
Java Programming: Exceptions1 Exceptions Reference: java.sun.com/docs/books/tutorial/essential/exceptions/
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Introduction to Exceptions in Java CS201, SW Development Methods.
Geoff Holmes Week 5 problems Handling Throwing Catching Multiple exceptions Finally clause Examples Exception Handling.
MIT AITI 2003 Lecture14 Exceptions
Exceptions 10-Nov-18.
Exceptions 10-Nov-18.
Handling Exceptions.
E x c e p t i o n s Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. — Martin Golding.
Exception Handling Chapter 9.
ATS Application Programming: Java Programming
Exception Handling Chapter 9 Edited by JJ.
Web Design & Development Lecture 7
Exceptions 19-Feb-19.
Java Exceptions Dan Fleck CS211.
Exceptions 25-Apr-19.
Java Programming Exceptions CSC 444 By Ralph B. Bisland, Jr.
Exceptions 22-Apr-19.
Exception Handling Contents
E x c e p t i o n s Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. — Martin Golding.
Exceptions 10-May-19.
Exceptions 5-Jul-19.
Presentation transcript:

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

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

© 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

© 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

© 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; // }

© 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; // }

© 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; // }

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

© 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

© 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 Foo Planned Crash and Burn Exception Tree! Foo

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

© 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

© 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

© 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 }

© 2001 by Ashby M. Woolf Revision 2 Throwable Error Type7Exception Type1Exception Exception Catching a Hierarchy of Exceptions Type3Exception Type2Exception Type4Exception IOException EOFException FileNotFoundException UnknownHostException 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

© 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

© 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

© 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; // }

© 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"); }

© 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; // }

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

© 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; // }

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

© 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

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

© 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"); }

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

© 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"); }

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

© 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"); }

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

© 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"); }

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

© 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; }

© 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

© 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(); }

© 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 Foo Throwable fillInStackTrace() String getLocalizedMessage() String getMessage() void printStackTrace() void printStackTrace(PrintStream s) void printStackTrace(PrintWriter s) String toString()

© 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( )

© 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

© 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( )

© 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

© 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(); }

© 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"); }

© 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( )

© 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

© 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

© 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

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

© 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

© 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

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