Download presentation
Presentation is loading. Please wait.
1
Chapter 6-3 (Book Chapter 8)
Introduction to OOP with Java 4th Ed, C. Thomas Wu Chapter 6-3 (Book Chapter 8) Exceptions Animated Version ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. © The McGraw-Hill Companies, Inc.
2
Propagating Exceptions
Intro to OOP with Java, C. Thomas Wu Propagating Exceptions Instead of catching a thrown exception by using the try-catch statement, we can propagate the thrown exception back to the caller of our method. The method header includes the reserved word throws. public int getAge( ) throws InputMismatchException { . . . int age = scanner.nextInt( ); return age; } Using the try-catch statement is the first way to handle the exceptions. The second way is to propagate the thrown exception back to the caller of the method. The method that includes the statement that calls our method must either catch it or propagate it back to their caller. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.
3
Intro to OOP with Java, C. Thomas Wu
Throwing Exceptions We can write a method that throws an exception directly, i.e., this method is the origin of the exception. Use the throw reserved to create a new instance of the Exception or its subclasses. The method header includes the reserved word throws. public void doWork(int num) throws Exception { . . . if (num != val) throw new Exception("Invalid val"); } It is possible to throw an exception from our own method. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.
4
Intro to OOP with Java, C. Thomas Wu
Exception Thrower When a method may throw an exception, either directly (by including a throw statement) or indirectly (by calling a method that throws an exception), we call the method an exception thrower. Every exception thrower must be one of two types: catcher. propagator. We say a method throws an exception directly when the method includes the throw statement. Otherwise, a method is throwing an exception indirectly. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.
5
Types of Exception Throwers
Intro to OOP with Java, C. Thomas Wu Types of Exception Throwers An exception catcher is a method that includes a matching catch block for the thrown exception. An exception propagator does not contain a matching catch block. A method may be a catcher of one exception and a propagator of another. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.
6
Intro to OOP with Java, C. Thomas Wu
Sample Call Sequence Method A public void B(){ try { C(); } catch (Exception e){ . . . } Method B t public void C() throws Exception{ D(); } } catch (Exception e){ . . . Method C public void D() throws Exception{ if (cond) { throw new Exception(); } Method D public void A(){ try { B(); } catch (Exception e){ . . . } catcher propagator propagator D C B This illustration shows a sequence of method calls among the exception throwers. Method D throws an instance of Exception. The green arrows indicate the direction of calls. The red arrows show the reversing of call sequence, looking for a matching catcher. Method B is the catcher. The call sequence is traced by using a stack. A Stack Trace ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.
7
©The McGraw-Hill Companies, Inc
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
8
©The McGraw-Hill Companies, Inc
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
9
Checked vs. Runtime Exceptions
Intro to OOP with Java, C. Thomas Wu Checked vs. Runtime Exceptions There are two types of exceptions: Checked. Unchecked. A checked exception is an exception that is checked at compile time. All other exceptions are unchecked, or runtime, exceptions. As the name suggests, they are detected only at runtime. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.
10
Java Exception Hierarchy (continued)
Unchecked Exceptions Java Programming: From Problem Analysis to Program Design, 4e
11
Java Exception Hierarchy (continued)
Unchecked Exceptions Java Programming: From Problem Analysis to Program Design, 4e
12
Java Exception Hierarchy (continued)
Checked Exceptions Java Programming: From Problem Analysis to Program Design, 4e
13
Handling Checked Exceptions
Intro to OOP with Java, C. Thomas Wu Handling Checked Exceptions When calling a method that can throw checked exceptions use the try-catch statement and place the call in the try block, or modify the method header to include the appropriate throws clause. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.
14
Handling Checked Exceptions
Intro to OOP with Java, C. Thomas Wu Handling Checked Exceptions Callers of a method that can throw a checked exception must include the try-catch statement in the method body or the throws clause in the header. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.
15
Handling Runtime Exceptions
When calling a method that can throw runtime exceptions, it is optional to use the try-catch statement or modify the method header to include a throws clause. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
16
Handling Runtime Exceptions
Intro to OOP with Java, C. Thomas Wu Handling Runtime Exceptions It is optional for callers of a method that can throw runtime exceptions to include the try-catch statement in the method body or the throws clause in the header. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.
17
User Defined Exception Example
class PrivateException extends Exception{ //constructor without parameters public PrivateException() {} //constructor for exception description public PrivateException(String description){ super(description); } class Test{ public Test(){} //this method will throw a PrivateException void method1(int a ,int b) throws PrivateException{ System.out.println("You called method1 !!!\n\n"); //specify when to generate the exception if(a==b){ String description="In method \"method1\" a PrivateException has been generated\n "+ " The "+a+" and "+b+" arguments are equal!!!\n"+ " Call this method with no equal arguments."; throw new PrivateException(description);}
18
User Defined Exception Example
//this method will throw a PrivateException void method2(int a, int b)throws PrivateException { System.out.println("You called method2 !!!\n\n"); //specify when to generate the exception if((a*b)>10) { String description="In method \"method2\" a PrivateException has been generated\n "+ " The "+a+" * "+b+" is grater that 10 !!!\n"+ " Call this method with smaller arguments."; throw new PrivateException(description); }
19
User Defined Exception Example
Stack Trace public class TestPrivateException{ public static void main(String[] args) { Test t= new Test(); try { t.method1(22,20);//NO EXCEPTION t.method2(11,23);//EXCEPTION } catch( PrivateException e) { e.printStackTrace(); PrivateException: In method "method2" a PrivateException has been generated The 11 * 23 is grater that 10 !!! Call this method with smaller arguments. at Test.method2(TestPrivateException.java:39) at TestPrivateException.main(TestPrivateException.java:59)
23
User Defined Exception Example1
public class E1 extends Exception { public E1(String mess) { super(mess);} } public class E2 extends Exception { public E2(String mess) {
24
Example1:Continued Public class TraceException { private double x; publicTraceException(double x) { this.x = x;} public doublegetX() { return x;} public void m1(double y) throws E1,E2 { System.out.println("Start of m1"); if (y == 0) throw new E1("m1 is null"); if (x * y < 0) throw new E2("opposite sign"); x = x / y; System.out.println("End of m1"); } public void m2(double y) throws E1 { System.out.println("Start of m2"); try { System.out.println("In m2 before m1"); m1(y); System.out.println("In m2 after m1"); } catch (E2 excpt) { System.out.println(excpt.getMessage()); } System.out.println("End of m2"); } public static void test(String s1,String s2) throws E1 { TraceException te = new TraceException(Double.parseDouble(s1)); te.m2(Double.parseDouble(s2)); System.out.println("x = " + te.getX()); } public static void main(String[] args) throws E1{ System.out.println("Start Of main"); try{ test("10","2"); test("10","-2"); test("10","0");} catch (Exception e){ System.out.println("propagated Exception catched"); } System.out.println("End of main"); }
25
The Output Start Of main Start of m2 In m2 before m1 Start of m1 End of m1 In m2 after m1 End of m2 x = 5.0 opposite sign x = 10.0 propagated Exception catched End of main
26
Example 2 Using the following Exception classes hierarchy, and the implementation of the class Check, write the method Method1 ( Check c ) of class Test as follow: Method1 (Check c) calls m1() and m2() methods of the c parameter object . In case the call of m1() throws an exception, Method1 continues as follows: • If the exception is of type E2, the exception message is displayed: • If the exception is of type E3, a stack trace message is displayed: • If the exception is of type E1 or E4, the exception is propagated to the calling method: Before the end of Method1, method m2() should be called (this statement MUST be executed in every scenario). Note: The header of Method1 should include only the required exception
27
Example 2 :Class Check public class Check { private double x; private double y; public Check(double x,double y) { this.x = x; this.y = y; } public double getX() { return x;} public double getY() { return y;} public void m1() throws E1,E2,E3 { System.out.println("Start of m1"); if (y == 0) throw new E1("y is null"); if (x * y < 0) throw new E2("opposite sign"); if (x/y < 1) throw new E3("x is smaller than y"); if (x==2*y) throw new E4 ("x is the double of y"); } public void m2() { System.out.println("I am in m2");} }
28
Example 2:Test Class public class Test{ public class Test{ public void method1(Check c)throws E1{ try{ c.m1(); } catch (E3 e){ e.printStackTrace(); } catch(E2 e){ e.getMessage(); } catch(E1 e){ throw new E1(e.getMessage()); } catch(E4 e){ throw new E4(e.getMessage()); } finally{ c.m2(); } } //Note: The place of the catching block of E4 is not important.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.