Chapter 6-3 (Book Chapter 8)

Slides:



Advertisements
Similar presentations
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 8 Exceptions and Assertions.
Advertisements

Exceptions Ensuring program reliability. Program correctness The term program correctness refers to a program’s working as advertised; that is, it produces.
Exception Handling Chapter 15 2 What You Will Learn Use try, throw, catch to watch for indicate exceptions handle How to process exceptions and failures.
Exception Handling and Format output
Java I/O Java I/O is based on input streams and output streams. All input and output are defined in the Java IO package. 1.
SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions.
بسم الله الرحمن الرحيم CPCS203: Programming II. Objectives After you have read and studied this chapter, you should be able to –Improve the reliability.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Exceptions Chapter 18 Programs: DriverException2 DriverException TestAgeInputException.
Chapter Chapter 8 Exceptions and Assertions.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
E XCEPTION H ANDLING Chapter 11 C S 442: A DVANCED J AVA P ROGRAMMING.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exceptions Handling.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
Chapter 8 Exceptions. Topics Errors and Exceptions try-catch throwing Exceptions Exception propagation Assertions.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exceptions and Assertions Animated Version.
06 - Exceptions. 2 ©S. Uchitel, 2004 A familiar sight? Bluescreen.scr.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Chapter 8  Errors and Exceptions Throwable class.
Exceptions and Assertions Recitation – 03/13/2009 CS 180 Department of Computer Science, Purdue University.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
Checked/Uncheck Exception And finally Block. Catching Any Exception It is possible to create a handler that catches any kind of exception. (This is possible.
Preventing and Correcting Errors
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Question of the Day  A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this possible?
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
Exceptions in Java. What is an exception? An exception is an error condition that changes the normal flow of control in a program Exceptions in Java separates.
EXCEPTIONS There's an exception to every rule.. 2 Introduction: Methods  The signature of a method includes  access control modifier  return type 
LECTURE 8: EXCEPTIONS CSC 212 – Data Structures. Error Handling Goals  What should we do when an error occurs?  Should alert system to the error  May.
Question of the Day completes starts  What word completes the 1 st word & starts the 2 nd one: DON???CAR.
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.
DCS 2133 Object Oriented Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Exception Handling.
CSE 1201 Object Oriented Programming
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
Introduction to OOP with Java 4th Ed, C. Thomas Wu
Chapter 13 Exception Handling
Exceptions: When things go wrong
Chapter 10 – Exception Handling
Tirgul 13 Exceptions 1.
MIT AITI 2003 Lecture14 Exceptions
Chapter 13 Exception Handling
Exception Handling and Format output-CS1050-By Gayani Gupta
Introduction to OO Program Design
Chapter 14: Exception Handling
Chapter 6-3 (Book Chapter 8)
Handling Exceptions.
Exception Handling Chapter 9.
ATS Application Programming: Java Programming
Exceptions & exception handling
ATS Application Programming: Java Programming
Exceptions & exception handling
Java Programming Language
Abdulmotaleb El Saddik University of Ottawa
Web Design & Development Lecture 7
Chapter 6-2 (Book Chapter 8)
Exception Handling in Java
Chapter 6-2 (Book Chapter 8)
Chapter 13 Exception Handling
Lecture 11 Objectives Learn what an exception is.
Errors and Exceptions Error Errors are the wrongs that can make a program to go wrong. An error may produce an incorrect output or may terminate the execution.
Chapter 12 Exception Handling and Text IO Part 1
Chapter 11 Inheritance and Polymorphism Part 1
Exceptions (part 2) December 3, 2007 ComS 207: Programming I (in Java)
Java Basics Exception Handling.
Java Programming: From Problem Analysis to Program Design, 4e
Basic Exception Handling
Presentation transcript:

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.

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.

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.

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.

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.

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.

©The McGraw-Hill Companies, Inc ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

©The McGraw-Hill Companies, Inc ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

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.

Java Exception Hierarchy (continued) Unchecked Exceptions Java Programming: From Problem Analysis to Program Design, 4e

Java Exception Hierarchy (continued) Unchecked Exceptions Java Programming: From Problem Analysis to Program Design, 4e

Java Exception Hierarchy (continued) Checked Exceptions Java Programming: From Problem Analysis to Program Design, 4e

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.

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.

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.

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.

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

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

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)

User Defined Exception Example1 public class E1 extends Exception { public E1(String mess) { super(mess);} } public class E2 extends Exception { public E2(String mess) {

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

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

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

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

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.