Chapter 10. Exception Handling. Java Actually: A Comprehensive Primer in Programming

Similar documents
More on Exception Handling

More on Exception Handling

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

Chapter 6. Arrays. Java Actually: A Comprehensive Primer in Programming

COE318 Lecture Notes Week 10 (Nov 7, 2011)

Programming II (CS300)

BBM 102 Introduction to Programming II Spring Exceptions

Programming II (CS300)

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong:

Exception Handling. Run-time Errors. Methods Failure. Sometimes when the computer tries to execute a statement something goes wrong:

Program Control Flow

Program Control Flow

C16b: Exception Handling

Object Oriented Programming Exception Handling

Introduction. Exceptions: An OO Way for Handling Errors. Common Runtime Errors. Error Handling. Without Error Handling Example 1

CS 3 Introduction to Software Engineering. 3: Exceptions

Chapter 12 Exception Handling

Exception-Handling Overview

Introduction to Software Design

BBM 102 Introduction to Programming II Spring 2017

Exception Handling in Java. An Exception is a compile time / runtime error that breaks off the

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

Fundamentals of Object Oriented Programming

COSC 123 Computer Creativity. I/O Streams and Exceptions. Dr. Ramon Lawrence University of British Columbia Okanagan

More on control structures

Object Oriented Programming

Exceptions and I/O: sections Introductory Programming. Errors in programs. Exceptions

Introductory Programming Exceptions and I/O: sections

Chapter 13 Exception Handling

CS115. Chapter 17 Exception Handling. Prof. Joe X. Zhou Department of Computer Science. To know what is exception and what is exception handling

Exceptions. What exceptional things might our programs run in to?

Exceptions in Java

What are Exceptions?

Exceptions (part 2) An exception is an object that describes an unusual or erroneous situation. Quick Review of Last Lecture.

About This Lecture. Outline. Handling Unusual Situations. Reacting to errors. Exceptions

Exceptions Chapter 10. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

CSC 1214: Object-Oriented Programming

Lecture 19 Programming Exceptions CSE11 Fall 2013

ECE 122. Engineering Problem Solving with Java

Chapter 14. Exception Handling and Event Handling ISBN

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

COMP-202. Exceptions. COMP Exceptions, 2011 Jörg Kienzle and others

Chapter 1. Getting started. Java Actually: A Comprehensive Primer in Programming

Exception Handling in Java

Java Errors and Exceptions. Because Murphy s Law never fails

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Designing Robust Classes

EXCEPTION HANDLING. Summer 2018

Lecture 20. Java Exceptional Event Handling. Dr. Martin O Connor CA166


Implementing Dynamic Data Structures

Exceptions and Error Handling

Exceptions, try - catch - finally, throws keyword. JAVA Standard Edition

CS Programming I: Exceptions

CS112 Lecture: Exceptions and Assertions

Introduction to Java. Handout-3a. cs402 - Spring

Sorting and searching arrays

Exception Handling. Chapter 11. Outline. Example: The Quotient app What Are Exceptions? Java By Abstraction Chapter 11

17. Handling Runtime Problems

Exceptions Handling Errors using Exceptions

Java Loose Ends. 11 December 2017 OSU CSE 1

Exceptions. Why Exception Handling?

CSCI 261 Computer Science II

Introduction to Computing II (ITI 1121) Final Examination

Object Communication

Basic Programming Elements

Notes on Java Programming IST JAV 2012 S. Frénot

File I/O and Exceptions

COMP 213. Advanced Object-oriented Programming. Lecture 17. Exceptions

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT

Exceptions - Example. Exceptions - Example

Exception in thread "main" java.lang.arithmeticexception: / by zero at DefaultExceptionHandling.main(DefaultExceptionHandling.

Software Practice 1 - Error Handling

ASSERTIONS AND LOGGING

Correctness and Robustness

COMP1008 Exceptions. Runtime Error

Recitation 3. 2D Arrays, Exceptions

COMP200 EXCEPTIONS. OOP using Java, based on slides by Shayan Javed

9. Java Errors and Exceptions

EXCEPTION-HANDLING INTRIVIEW QUESTIONS

CS Programming I: Exceptions

Introduction to Computer Science II CS S-22 Exceptions

Advanced Flow of Control -- Introduction

Std 12 Lesson-10 Exception Handling in Java ( 1

Exceptions: When something goes wrong. Image from Wikipedia

School of Informatics, University of Edinburgh

Reminder. Topics CSE What Are Exceptions?! Lecture 11 Exception Handling

Polymorphism and Interfaces

Typecasts and Dynamic Dispatch. Dynamic dispatch

Tutorial 8 Date: 15/04/2014

Exceptions. Examples of code which shows the syntax and all that

National University. Faculty of Computer Since and Technology Object Oriented Programming

Exceptions. COMP 202 Exceptions. Exceptions. Exceptions. An exception is an object that describes an unusual or erroneous situation

ITI Introduction to Computing II

Topic 6: Exceptions. Exceptions are a Java mechanism for dealing with errors & unusual situations

For more details on SUN Certifications, visit

Transcription:

Chapter 10 Exception Handling Lecture slides for: Java Actually: A Comprehensive Primer in Programming Khalid Azim Mughal, Torill Hamre, Rolf W. Rasmussen Cengage Learning, 2008. ISBN: 978-1-844480-933-2 http://www.ii.uib.no/~khalid/jac/ Permission is hereby granted to use these lecture slides in conjunction with the book. Modified: 11/12/09 Java Actually 10: Exception Handling 10-1/28

Overview Program Execution Throw-and-catch principle Exception Propagation Typical scenarios when using the try-catch statement Using the throws clause with checked exceptions Unchecked exceptions Java Actually 10: Exception Handling 10-2/28

What is an exception? An exception in Java signals an error situation that can during program execution. Some examples of error situations: 1. Programming errors -- occur because of logical errors. Illegal array index Integer division by 0 Method call with illegal parameters Using a reference with the null value to access members of an object 2. Runtime error -- programmer has little control over. Opening a file that doesn t exist. Read or write errors when using a file. A network connection that goes down. Java Actually 10: Exception Handling 10-3/28

Program execution A program stack is used to manage the execution of methods. An element (called the stack frame) on the program stack corresponds to a method call. Each method call results in the creation of a new stack frame. Stack frame contains various information, including storage for local variables. When the method is complete, its frame is removed from the program stack. The method that has the stack frame on the top of the program stack is executed. When returning from a method call, program execution continues with the method in the corresponding stack frame that now has been revealed on top of the program stack. During execution, all stack frames on the program stack at any given time, specify which methods are active, i.e., have not finished executing. Java Actually 10: Exception Handling 10-4/28

Method Execution and Exception Propagation We shall use the following problem to illustrate the method of execution and exception propagation. Calculate the speed when the distance and time are given. The program uses three methods: 1. main() 2. printspeed() 3. calculatespeed() Note integer division in the calculation of expression (distance / time). Integer division by 0 is an illegal operation in Java. Java Actually 10: Exception Handling 10-5/28

public class Speed1 { Method Execution (Program 10.1) public static void main(string[] args) { System.out.println("Entering main()."); printspeed(100, 20); // (1) System.out.println("Returning from main()."); private static void printspeed(int kilometers, int hours) { System.out.println("Entering printspeed()."); int speed = calculatespeed(kilometers, hours); // (2) System.out.println("Speed = " + kilometers + "/" + hours + " = " + speed); System.out.println("Returning from printspeed()."); private static int calculatespeed(int distance, int time) { System.out.println("Calculating speed."); return distance/time; // (3) Java Actually 10: Exception Handling 10-6/28

Normal execution (Figure 10.1) Speed1 System.out main(...) args =... println("entering main().") printspeed(100,20) // (1) kilometers = 100 hours = 20 println("entering printspeed().") calculatespeed(100,20) // (2) distance = 100 time = 20 println("calculating speed.") speed = 5 100/20 return 5 // (3) println("speed = 100 / 20 = 5") println("returning from printspeed().") println("returning from main().") Method execution Program output: Entering main(). Entering printspeed(). Calculating speed. Speed = 100/20 = 5 Returning from printspeed(). Returning from main(). Java Actually 10: Exception Handling 10-7/28

Throw-and-catch principle An exception is thrown when an error situation arises during program execution, and is caught by an exception handler that handles it. Replace in Program 10.1: printspeed(100, 20); // (1) with printspeed(100, 0); // (1) Second parameter is 0. Execution of the program is illustrated in the sequence diagram in Figure 10.2. In (7) an ArithmeticException is thrown-exception, which is communicated back (propagated) through the stack frames of the program stack. Java Actually 10: Exception Handling 10-8/28

Exception Propagation (integer division by 0) (Figure 10.2) main(...) Speed1 args =... println("entering main().") printspeed(100,0) // (1) System.out kilometers = 100 hours = 0 println("entering printspeed().") calculatespeed(100,0) // (2) distance = 100 time = 0 100/0 exception propagation println("calculating speed.") // (3) :ArithmeticExeception "/ by zero" Program output: Entering main(). Entering printspeed(). Calculating speed. Printed by the program Printed by the default exception handler Exception in thread "main" java.lang.arithmeticexception: / by zero at Speed1.calculateSpeed(Speed1.java:20) at Speed1.printSpeed(Speed1.java:12) at Speed1.main(Speed1.java:6) class name method name file name line number in the file where the call to the next method occurred. Java Actually 10: Exception Handling 10-9/28

Exception Propagation Program execution does not continue normally during unntakspropageringen, and the exception is not returned by the return statement. The exception is offered to active methods in turn. If an active method does not catch the exception, the execution of the method is interrupted, i.e. its stack frame is removed from the program stack. When the exception propagates to the "top level", it is processed by a standard exception handler in the Java virtual machine. Standard exception handler generates a stack trace at the terminal. Java Actually 10: Exception Handling 10-10/28

Exception Handling: try-catch A try block can contain arbitrary code, but the purpose is to add code that might throw an exception during execution. A catch block is an exception handler. A catch block belongs with a try block. An exception that can be thrown as a result of executing the code in the try block, can be caught and handled in a corresponding catch block. The try block contains the code that can lead to an exception being thrown. try block catch block try { int speed = calculatespeed(kilometers, hours); System.out.println("Speed = " + kilometers + "/" + hours + " = " + speed); catch (ArithmeticException exception) { one catch block parameter System.out.println(exception + " (handled in printspeed())"); A catch block can catch an exception and handle it, if it is of the right type. Java Actually 10: Exception Handling 10-11/28

Typical scenarios when using a try-catch statement 1. The code in the try block is executed, and no exception is thrown. Normal execution continues after the try-catch blocks. 2. The code in the try block is executed, and an exception is thrown. This exception is caught and handled in a corresponding catch block. The execution of the try-block is cancelled, such that the actions in the rest of the try block are not executed. Normal execution continues after the try-catch blocks. 3. The code in the try block is executed, and an exception is thrown, but no catch block is found to handle the exception. The execution of the try-block is cancelled, such that the actions in the rest of the try block are not executed. The exception is propagated. Java Actually 10: Exception Handling 10-12/28

try-catch scenarios (Figure 10.4) Execute try-block [exception] Find catch-block [no catch-block found]... [no exception] 1 2 3 [exception1] [exception2] [exceptionn] Execute catch-block for exception1 Execute catch-block for exception2... Execute catch-block for exceptionn No exception or exception handled. Normal execution continues afer the try-catch-blocks. Exception not handled. Execution aborted and exception propagated. Java Actually 10: Exception Handling 10-13/28

try-catch scenario 1 In Program 10.2, the printspeed() method uses a try-catch statement, (2). The corresponding catch block, (4), is declared to catch exceptions of type ArithmeticException. Execution of Program 10.2 is shown in Figure 10.5. Java Actually 10: Exception Handling 10-14/28

Exception handling (Program 10.2) public class Speed2 { public static void main(string[] args) { System.out.println("Entering main()."); printspeed(100, 20); // (1) System.out.println("Returning from main()."); private static void printspeed(int kilometers, int hours) { System.out.println("Entering printspeed()."); try { // (2) int speed = calculatespeed(kilometers, hours); // (3) System.out.println("Speed = " + kilometers + "/" + hours + " = " + speed); catch (ArithmeticException exception) { // (4) System.out.println(exception + " (handled in printspeed())"); System.out.println("Returning from printspeed()."); private static int calculatespeed(int distance, int time) { System.out.println("Calculating speed."); return distance/time; // (5) Java Actually 10: Exception Handling 10-15/28

Exception handling (Figure 10.5) (scenario 1) main(...) Speed2 args =... println("entering main().") printspeed(100,20) // (1) System.out kilometers = 100 hours = 20 try { println("entering printspeed().") calculatespeed(100,20) // (3) distance = 100 time = 20 100/20 println("calculating speed.") return 5 // (5) speed = 5 println("speed = 100 / 20 = 5") println("returning from printspeed().") println("returning from main().") Program output: Entering main(). Entering printspeed(). Calculating speed. Speed = 100/20 = 5 Returning from printspeed(). Returning from main(). Java Actually 10: Exception Handling 10-16/28

Replace in Program 10.2: printspeed(100, 20); // (1) with try-catch scenario 2 printspeed(100, 0); // (1) Second parameter is 0. Integer Division by 0 causes an ArithmeticException exception to be thrown at (5) in the method caculatespeed(). Execution of this method is interrupted and the exception is propagated. It is caught by the catch block in method printspeed(). After handling the exception, normal execution of the program is restored. Printout shows the normal execution of the printspeed() method from this point. Java Actually 10: Exception Handling 10-17/28

Exception handling (Figure 10.6) (scenario 2) main(...) Speed2 args =... println("entering main().") printspeed(100,0) // (1) System.out kilometers = 100 hours = 0 try { println("entering printspeed().") calculatespeed(100,0) // (3) distance = 100 time = 0 println("calculating speed.") 100/0 :ArithmeticException catch(arithmetic- Exception "/ by zero" exception) { println(exception + " (handled in printspeed())") println("returning from printspeed().") println("returning from main().") Program output: Entering main(). Entering printspeed(). Calculating speed. java.lang.arithmeticexception: / by zero (handled in printspeed()) Returning from printspeed(). Returning from main(). Java Actually 10: Exception Handling 10-18/28

try-catch scenario 3 Scenario 3 shows what happens when an exception is thrown during the execution of a try-block and there is no corresponding catch block to handle the exception. The program's execution behavior in Figure 10.7 shows that the integer division with 0 again leads to an ArithmeticException-exception, thrown at (7) in the method caculatespeed(). Execution of this method is interrupted and the exception is propagated. ArithmeticException is not caught by the catch block in printspeed() method, since this catch block can only handle exceptions of type IllegalArgumentException. Execution of the printspeed() method is interrupted, and the exception is propagated. The exception ArithmeticException is now caught by the catch block in method main() at (3). After handling the exception in the catch block in method main(), normal execution of the program is restored. Printout shows the normal execution of the method main() from this point. Java Actually 10: Exception Handling 10-19/28

Exception handling (Program 10.3) (scenario 3) public class Speed3 { public static void main(string[] args) { System.out.println("Entering main()."); try { // (1) printspeed(100,20); // (2) catch (ArithmeticException exception) { // (3) System.out.println(exception + " (handled in main())"); System.out.println("Returning from main()."); private static void printspeed(int kilometers, int hours) { System.out.println("Entering printspeed()."); try { // (4) int speed = calculatespeed(kilometers, hours); // (5) System.out.println("Speed = " + kilometers + "/" + hours + " = " + speed); catch (IllegalArgumentException exception) { // (6) System.out.println(exception + " (handled in printspeed())"); System.out.println("Returning from printspeed()."); Java Actually 10: Exception Handling 10-20/28

private static int calculatespeed(int distance, int time) { System.out.println("Calculating speed."); return distance/time; // (7) Java Actually 10: Exception Handling 10-21/28

Exception handling (Figure 10.7) (scenario 3) main(...) args =... try { Speed3 println("entering main().") printspeed(100,0) // (2) System.out kilometers = 100 hours = 0 try { println("entering printspeed().") calculatespeed(100,0) // (5) distance = 100 time = 0 println("calculating speed.") catch(arithmeticexception exception) { 100/0 :ArithmeticException "/ by zero" println(exception + " (handled in main())") println("returning from main().") Program output: Entering main(). Entering printspeed(). Calculating speed. java.lang.arithmeticexception: / by zero (handled in main()) Returning from main(). Java Actually 10: Exception Handling 10-22/28

Handling of controlled exceptions The purpose of the exception handling is to deal with error situations during program execution. It is possible for a method that throws an exception, to let the exception propagate further without doing something about it. This results in applications that are not very robust. Use of controlled exception provides a strategy in which a method, that can throw such an exception, is forced to decide how the exception should be handled: 1. Catch and handle the exception in a try-catch statement. 2. Allow further propagation of the exception with a throws-clause specified in the method declaration. A throws clause is specified in the method header, between the parameter list and the method body:... method name (...) throws exception class 1,..., exception class n {... Java Actually 10: Exception Handling 10-23/28

Handling of controlled exceptions Given that the method foo() calls the method bar(): void bar () throws { /*...*/ // K is a checked exception. void foo () { bar (); // Compile-time error. Solution: Either void foo () throws K { bar (); // Throws the exception further. or: void foo () { try { bar (); catch (K k) { k.printstacktrace (); // Catch and handle the exception. The compiler will check that a method that can throw a checked exception meets one of the above requirements. This means that any client of a method that can propagate a checked exception in a throws clause, must decide how this exception should be handled. If a checked exception specified in a throws clause propagates all the way to the top level, it will be processed by a standard exception handler in the normal way. Java Actually 10: Exception Handling 10-24/28

Example: checked exceptions (Program 10.4) The method caculatespeed() explicitly throws an Exception in an if statement, (6), using the throw statement. This exception is further propagated in a throws-clause (5). The method printspeed (), which calls the method caculatespeed(), also chooses to propagate the exception in a throws-clause (4). The method main() that calls the printspeed() method, chooses to catch and handle this exception in a try-catch block, (1) and (3). Normal execution continues after the exception is handled. Attempts to exclude the throws-clauses will result in compile-time errors. Java Actually 10: Exception Handling 10-25/28

public class Speed6 { Handling checked exceptions (Program 10.4) public static void main(string[] args) { System.out.println("Entering main()."); try { //(1) // printspeed(100, 20); //(2a) printspeed(-100,20); //(2b) catch (Exception exception) { //(3) System.out.println(exception + " (handled in main())"); System.out.println("Returning from main()."); private static void printspeed(int kilometers, int hours) throws Exception { //(4) System.out.println("Entering printspeed()."); double speed = calculatespeed(kilometers, hours); System.out.println("Speed = " + kilometers + "/" + hours + " = " + speed); System.out.println("Returning from printspeed()."); Java Actually 10: Exception Handling 10-26/28

private static int calculatespeed(int distance, int time) throws Exception { //(5) System.out.println("Calculating speed."); if (distance < 0 time <= 0) //(6) throw new Exception("distance and time must be > 0"); return distance/time; Running the program with (2a): Entering main(). Entering printspeed(). Calculating speed. Speed = 100/20 = 5.0 Returning from printspeed(). Returning from main(). Running the program with (2b): Entering main(). Entering printspeed(). Calculating speed. java.lang.exception: distance and time must be > 0 (handled in main()) Returning from main(). Java Actually 10: Exception Handling 10-27/28

Unchecked Exceptions Unchecked exceptions are exceptions that the compiler does not check, i.e. the compiler does not care whether the code deals with them or not. Unchecked exceptions are usually the result of programming errors and should be corrected. The unchecked AssertionError should never be caught as it thrown by the assert statement. See Table 10.2. Java Actually 10: Exception Handling 10-28/28