Chapter 15 – Exception Handling

Slides:



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

Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
Exceptions Ensuring program reliability. Program correctness The term program correctness refers to a program’s working as advertised; that is, it produces.
CSM-Java Programming-I Spring,2005 Exceptions Lesson - 7.
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
An Introduction to Java Programming and Object- Oriented Application Development Chapter 8 Exceptions and Assertions.
COMP 121 Week 5: Exceptions and Exception Handling.
Chapter 9 Exception Handling. Chapter Goals To learn how to throw exceptions To be able to design your own exception classes To understand the difference.
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.
When you use an input or output file that does not exist, what will happen? −The compiler insists that we tell it what the program should do in such case.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Exceptions Chapter 18 Programs: DriverException2 DriverException TestAgeInputException.
EXCEPTIONS. What’s an exception?? Change the flow of control when something important happens ideally - we catch errors at compile time doesn’t happen.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
Chapter 11.  Data is often stored in files such as a text file  We need to read that data into our program  Simplest mechanism  Scanner class  First.
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.
©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.
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.
Chapter 8 Exceptions. Topics Errors and Exceptions try-catch throwing Exceptions Exception propagation Assertions.
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.
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.
©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.
Preventing and Correcting Errors
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.
Chapter 14 Exception Handling. Chapter Goals To learn how to throw exceptions To be able to design your own exception classes To understand the difference.
Exceptions and Assertions Chapter 15 – CSCI 1302.
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Exception Handling.
Chapter 10 – Exception Handling
OBJECT ORIENTED PROGRAMMING II LECTURE 10 GEORGE KOUTSOGIANNAKIS
MIT AITI 2003 Lecture14 Exceptions
Introduction Exception handling Exception Handles errors
CS102 – Exceptions David Davenport Latest: May 2015
CSE 501N Fall ’09 17: Exception Handling
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.
EE422C Software Implementation II
Exception Handling Chapter 9.
Exceptions & exception handling
Exceptions & exception handling
Exception Handling Chapter 9 Edited by JJ.
Exception Handling.
Web Design & Development Lecture 7
Chapter 11 Input/Output Exception Handling
Exceptions 19-Feb-19.
Chapter 12: Exceptions and Advanced File I/O
Exceptions 7-Apr-19.
CMSC 202 Exceptions 2nd Lecture.
Exceptions 25-Apr-19.
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.
Java Programming Exceptions CSC 444 By Ralph B. Bisland, Jr.
Tutorial Exceptions Handling.
Exceptions 22-Apr-19.
Chapter 12 Exception Handling and Text IO Part 1
CSC 143 Java Errors and Exceptions.
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.
Java Basics Exception Handling.
Exceptions 5-Jul-19.
Throwing, Catching Defining
Presentation transcript:

Chapter 15 – Exception Handling

15.1 Throwing Exceptions What is a good program? A program that is reliable? Not just giving correct answer on correct input Should protect against possible errors (invalid password, not a picture file, etc)

Good programs Need a robust program, reliable in the eyes of a novice user It should not (“cannot”) crash easily - what if Word always crashed when you hit the control key?

Good Programs How do we ensure a robust program? Option 1 – return a special value to determine if the method succeeded or failed Ex. Make withdraw return true or false What’s wrong with this? Calling method may not check answer Calling method may not know what to do

Good Programs Option 2 - Use exceptions Exception – an error condition that can occur during the normal course of a program execution Exception handling is another form of control structure (like ifs and switch statements) When an error is encountered, the normal flow of the program is stopped and the exception is handled

Exceptions We say an exception is thrown when it occurs When the exception-handling code is executed, the error is caught Examples: Dividing by zero Accessing a null object

Exceptions What happens when exceptions occur? An Exception object is thrown What happens when an Exception is thrown? normal execution stops and exception handling begins What does the Exception object know? the name of the problem the location where it occurred and more…

Exceptions Why use exceptions? consistency (everyone else does it) Java API classes use exceptions. Other programming languages do too! flexibility programmer can decide how to fix problems. simplicity Easy to pinpoint problems.

Types of Exceptions Null Pointer Exception FoodIterator fi = afc.getAllFood(); while ( !hasFood() && fi.hasNext() { Food f = fi.getNextFood(); if (f.getLocation().equals(myLoc)) myFood=f; }

Types of Exceptions Arithmetic Exception: Divide by zero public double divide( int data1, int data2 ) { return data1 / data2; }

Handling Exceptions Do nothing Propagate (throw) it program crashes if the exception occurs! Propagate (throw) it tell the method’s caller about it and let them decide what to do Resolve (try-catch) it in our method fix it or tell the user about it and let them decide what to do

Handling Exceptions So far, we have let the system handle exceptions int score = in.nextInt(); If the user enters “abc123” Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:819) at java.util.Scanner.next(Scanner.java:1431) at java.util.Scanner.nextInt(Scanner.java:2040) at java.util.Scanner.nextInt(Scanner.java:2000) at Test.main(Test.java:5)

Handling Exceptions Says in English: System has caught an error described as a InputMismatchException Thrown because a String cannot be converted to an integer When system handles, we often get a program crash Instead of the system, we can handle to improve robustness

Handling Exceptions Better solution: Instead of letting the program crash, use Exception Handling to improve program’s robustness Exceptions cannot be ignored Exceptions are handled by Exception Handlers directly

Throwing Exceptions If there is an error in the value of a parameter, we can throw exceptions to make the user accountable Decide what type of exception to throw Test for condition, and throw the exception if condition is violated

Example Throw an exception object to signal an exceptional condition Example: What do we do if the amount to withdraw is greater than the balance? IllegalArgumentException: illegal parameter value

Problem public class BankAccount { public void withdraw(double amount) if (amount > balance) ????????? } balance = balance - amount; . . .

Solution #1 public class BankAccount { public void withdraw(double amount) if (amount > balance) IllegalArgumentException exception = new IllegalArgumentException("Amount exceeds balance"); throw exception; } balance = balance - amount; . . .

Solution #2 public class BankAccount { public void withdraw(double amount) if (amount > balance) throw new IllegalArgumentException("Amount exceeds balance"); } balance = balance - amount; . . .

Throwing Exceptions: Syntax throw exceptionObject;

Checked/Unchecked Exceptions Checked Exception – checked at compile time Complier ensures that you are handling a possible problem These errors are due to external circumstances that the programmer cannot prevent Majority occur when dealing with input and output For example, IOException

Checked/Unchecked Exceptions Unchecked Exception = Runtime Exceptions Extend the class RuntimeException or Error They are the programmer's fault Examples of runtime exceptions: NumberFormatException IllegalArgumentException NullPointerException Optional to deal with these Example of error: OutOfMemoryError Can’t do anything about these catastrophic problems, so don’t deal with it

Checked/Unchecked Exceptions Unchecked Exceptions result from deficiencies in your code, so should check on own Null object reference Sending a negative value to Math.sqrt() Checked Exceptions are not the fault of the coder Problems with the file format, user input, etc.

Checked/Unchecked Exceptions Categories aren't perfect: Scanner.nextInt throws unchecked InputMismatchException Programmer cannot prevent users from entering incorrect input This choice makes the class easy to use for beginning programmers

Checked/Unchecked Exceptions Deal with checked exceptions principally when programming with files and streams For example, use a Scanner to read a file String filename = . . .; FileReader reader = new FileReader(filename); Scanner in = new Scanner(reader); But, FileReader constructor can throw a FileNotFoundException

Handling Checked Exceptions Handle the exception Tell compiler that you want method to be terminated when the exception occurs Use throws specifier so method can throw a checked exception public void read(String filename) throws FileNotFoundException { FileReader reader = new FileReader(filename); Scanner in = new Scanner(reader); . . . }

Handling Checked Exceptions throws tells the compiler to “pass the buck” to the method that called this method Can propagate multiple exceptions: public void read(String filename) throws IOException, ClassNotFoundException Can also group using hierarchy If method can throw an IOException and FileNotFoundException, only use IOException

Handling Checked Exceptions Why propagate when we could handle the error ourselves? We may not know how to Let user of my code decide Better to declare exception than to handle it incompetently

Catching Exceptions At some point, an exception should be dealt with If not, program terminates with error message Professional code requires more sophistication – cannot just allow errors to kill program What would happen if all of my.wisc.edu turned off if you entered wrong password?

Catching Exceptions To deal with this problem, install exception handlers in your code to deal with possible exceptions Handlers are try/catch statements

Try-Catch Put statement(s) that could cause an error in the try block Error handling code goes in catch block Only is executed if there was an error Can have multiple catch blocks, one for each possible type of exception

try-catch Syntax try { <try block> } catch ( <ExceptionClass> <name> ) <catch block> }…

try { String filename = . . .; FileReader reader = new FileReader(filename); Scanner in = new Scanner(reader); String input = in.next(); int value = Integer.parseInt(input); . . . } catch (IOException exception) exception.printStackTrace(); catch (NumberFormatException exception) System.out.println("Input was not a number");

Example 3 types of error can be thrown FileNotFoundException is thrown by FileReader constructor  caught by IOException clause NoSuchElementException is thrown by Scanner.next  not caught, thrown to caller NumberFormatException is thrown by Integer.parseInt()  caught by second clause

Catching Exceptions If there are no errors, the catch block is skipped If an exception is thrown, the try block stops executing immediately and jumps to the catch block

Catching Exceptions

Exception Information Why do we have Exception objects? We can get information on where the error happened and what exactly happened Exception objects have 2 methods defined: getMessage() (what happened?) printStackTrace() (where did it happen?)

getMessage Returns the data that cause the error … Example: }catch(NumberFormatException e){ System.out.println(e.getMessage()); }

printStackTrace Prints out a trace of methods that caused the error starting at the root of the error Where did the exception occur? What method called this code to execute..etc. This is what the system does when an exception is thrown Example: … }catch(NumberFormatException e){ e.printStackTrace(); }

Catching ALL Exceptions public double average( String data ) { int sum = 0; for ( int i=0; i < data.length(); i++ ) sum += Integer.parseInt(data.charAt(i)); return sum/data.length(); } try { } catch ( Exception e ) // catch ALL exceptions { System.out.println( e ); return 0; } What exceptions can occur in this code fragment? NullPointerException ArithmeticException: / By Zero

Catching ALL Exceptions Why is this probably a bad idea? Probably want to handle each type of exception differently Don't want to catch things like NullPointerExeption

Catching Exceptions IMPORTANT! Order of catch blocks matters } catch (Exception e){ System.out.println(e.getMessage()); } catch (NumberFormatException e){ System.out.println("'" + str + "'not valid input, Please use digits only"); } You should go specific to generic NumberFormatException is a specific type of the class Exception (inheritance)

Demo p.s.v. main( String [] args ) { try { String filename = args[0], input; scanner = new Scanner( new File(filename) ); input = scanner.nextLine(); int value = Integer.parseInt( input ); double reciprocal = 1.0 / value; System.out.println( "rec=" + rec ); } catch ( Exception e ) S.o.pln( "Found Exception: " + e ); S.o.pln( "Done" ); What exceptions can occur in this code fragment? IOException NumberFormatException ArithmeticException – DivideByZero NegativeArraySizeException

String filename=args[0], input; try { scanner = new Scanner(new File(filename)); input = scanner.nextLine(); int value = Integer.parseInt( input ); double reciprocal = 1.0 / value; System.out.println( "rec=" + rec ); } catch ( NumberFormatException e ) { S.o.pln( input + " is not an integer"); catch ( ArithmeticException e ) { S.o.pln( "Can't divide by zero" ); catch ( FileNotFoundException e ) { S.o.pln( filename + " Not Found" ); finally { S.o.pln( "Done" ); } What exceptions can occur in this code fragment? IOException NumberFormatException ArithmeticException – DivideByZero NegativeArraySizeException

Catching Exceptions What if an exception is thrown and there is no catch block to match? The system handles it (terminates program and prints the stack trace)

The finally clause Recall that whenever an exception occurs, we immediately go to the catch block What if there is some code we want to execute regardless of whether there was an exception? finally block is used

The finally clause try{ distance = Double.parseDouble(str); if (distance < 0){ throw new Exception("Negative distance is not valid"); } return distance; } catch (NumberFormatException e){ System.out.println("'" + str + "'not valid input, Please use digits only"); } catch (Exception e){ System.out.println(e.getMessage()); } finally { System.out.println(“Done”);

The finally clause finally is executed NO MATTER WHAT Even if there is a break or return in the try block Good for “cleanup” of a method

Exception Inheritance Hierarchy Throwable Exception Exception Inheritance Hierarchy RuntimeException IOException NullPointerException ArithmeticException NegativeArrayIndexException IndexOutOfBoundsException IllegalArgumentException ArrayIndexOutOfBoundsException StringIndexOutOfBoundsException Create Exception Hierarchy Handout (online and hardcopy for class) Link to this for "Java Exceptions" web page. NumberFormatException FileNotFoundException EOFException

try-catch Control Flow code before try try block code after try no exceptions occur

try-catch Control Flow code before try try block catch block exception occurs code after try

try-catch Control Flow code before try try block catch block exception occurs code after try no exceptions occur

try-catch Control Flow code after try code before try try block no exceptions occurred finally block (if it exists)

try-catch Control Flow code after try code before try try block exception occurs catch block finally block (if it exists)

try-catch Control Flow code before try exception occurs matches first catch block? true try block no exceptions occurred 1st catch block false matches next catch block? 2nd catch block true catch block must match the class of exception that occurs false finally block (if it exists) code after try

Propagating Exceptions When a method may throw an exception, either directly or indirectly, we call the method an exception thrower. Every exception thrower must be one of two types: a catcher, or a propagator.

Propagating Exceptions An exception catcher is an exception thrower 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.

Example The following figure 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.

Example

Propagating Exceptions If a method is an exception propagator, we need to modify its header to declare the type of exceptions the method propagates. We use the reserved word throws for this declaration. public void C( ) throws Exception { ... } public void D( ) throws Exception {

Propagating Exceptions Without the required throws Exception clause, the program will not compile. However, for exceptions of the type called runtime exceptions, the throws clause is optional.

Propagating vs. Catching Do not catch an exception that is thrown as a result of violating a condition set by the client programmer. Instead, propagate the exception back to the client programmer’s code and let him or her handle it.

Exceptions in a Constructor We can thrown an exception in the constructor if a condition is violated For example, we may throw an IllegalArgumentException if a parameter value is invalid