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.

Slides:



Advertisements
Similar presentations
Pearson Education, Inc. All rights reserved. 1.. Exception Handling.
Advertisements

Topics Introduction Types of Errors Exceptions Exception Handling
Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
Exceptions Ensuring program reliability. Program correctness The term program correctness refers to a program’s working as advertised; that is, it produces.
CS102--Object Oriented Programming
An Introduction to Java Programming and Object- Oriented Application Development Chapter 8 Exceptions and Assertions.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 15 – Exception Handling Outline 15.1 Introduction 15.2 Exception-Handling Overview 15.3 Exception-Handling.
Exception Handling and Format output
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
Objectives In this chapter you will: Learn what an exception is Learn how to handle exceptions within a program See how a try / catch block is used to.
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
 Pearson Education, Inc. All rights reserved Exception Handling.
SE-1020 Dr. Mark L. Hornick 1 Exceptions and Exception Handling.
Exception Handling 1 CISC6795, Spring Introduction 2 Exception – an indication of a problem that occurs during a program’s execution, for examples:
 2002 Prentice Hall, Inc. All rights reserved. Chapter 14 – Exception Handling Outline 14.1 Introduction 14.2 When Exception Handling Should Be Used 14.3.
 2005 Pearson Education, Inc. All rights reserved Exception Handling.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Exception Handling 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
E XCEPTION H ANDLING Chapter 11 C S 442: A DVANCED J AVA P ROGRAMMING.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
 2005 Pearson Education, Inc. All rights reserved Exception Handling.
Chapter 8 Exceptions. Topics Errors and Exceptions try-catch throwing Exceptions Exception propagation Assertions.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Chapter 8  Errors and Exceptions Throwable class.
Chapter 11: Handling Exceptions and Events J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Fourth.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
Java Software Solutions Foundations of Program Design Sixth Edition
Exception Handling. Exceptions and Errors When a problem encounters and unexpected termination or fault, it is called an exception When we try and divide.
Exception Handling 1. Introduction Users may use our programs in an unexpected ways. Due to design errors or coding errors, our programs may fail in unexpected.
Object Oriented Programming
06 Exception Handling. 2 Contents What is an Exception? Exception-handling in Java Types of Exceptions Exception Hierarchy try-catch()-finally Statement.
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
CIS 270—Application Development II Chapter 13—Exception Handling.
Chapter 12: Exception Handling
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
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 10.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
Exception Handling Unit-6. Introduction An exception is a problem that arises during the execution of a program. An exception can occur for many different.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
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.
Unit 4 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
Exceptions and Assertions Chapter 15 – CSCI 1302.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
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.
Exception Handling in C++. Outline What exceptions are and when to use them Using try, catch and throw to detect, handle and indicate exceptions, respectively.
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Exception Handling. You learned that there are three categories of errors: syntax errors, runtime errors, and logic errors. Syntax errors arise because.
Exceptions in the Java programming language J. W. Rider.
Appendix H Exception Handling: A Deeper Look
16 Exception Handling.
13 Exception Handling.
Chapter 14: Exception Handling
Exception Handling Chapter 9.
ATS Application Programming: Java Programming
Exception Handling Chapter 9 Edited by JJ.
Lecture 11 Objectives Learn what an exception is.
Exception Handling.
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 15 – Exception Handling
Presentation transcript:

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.

3 Motivation  We seek robust programs  When something unexpected occurs  Ensure program detects the problem  Then program must do something about it  Extensive testing of special situations can result in "spaghetti code"  Need mechanism to check for problem where it could occur  When condition does occur  Have control passed to code to handle the problem

4 Overview  Exception  Indication of problem during execution  Uses of exception handling  Process exceptions from program components  Handle exceptions in a uniform manner in large projects  Remove error-handling code from “main line” of execution  A method detects an error and throws an exception  Exception handler processes the error  Uncaught exceptions yield adverse effects  Might terminate program execution

5 Overview  Code that could generate errors put in try blocks  Code for error handling enclosed in a catch clause  The finally clause always executes  Termination model of exception handling  The block in which the exception occurs expires  throws clause specifies exceptions method throws

6 Exception Handler Exception "thrown" here Exception handler Thrown exception matched against first set of exception handlers If it fails to match, it is matched against next set of handlers, etc. If exception matches none of handlers, program is abandoned

7 Terminology  Thrown exception – an exception that has occurred  Stack trace  Name of the exception in a descriptive message that indicates the problem  Complete method-call stack  ArithmeticException – can arise from a number of different problems in arithmetic

8 Terminology  Throw point – initial point at which the exception occurs, top row of call chain  InputMismatchException – occurs when Scanner method nextInt receives a string that does not represent a valid integer  See Example, Figure 13.1 Figure 13.1Figure 13.1

9 Termination Model of Exception Handling  When an exception occurs:  try block terminates immediately  Program control transfers to first matching catch block  try statement – consists of try block and corresponding catch and/or finally blocks

10 Contrast  Termination model  program control does not return to the throw point  try block has expired;  Flow of control proceeds to the first statement after the last catch block  Resumption model  program control resumes just after throw point

11 Enclosing Code in a try Block  try block – encloses code that might throw an exception and the code that should not execute if an exception occurs  Consists of keyword try followed by a block of code enclosed in curly braces

12 Using the throws Clause  Appears after method’s parameter list and before the method’s body  Contains a comma-separated list of exceptions

13 Using the throws Clause  Exceptions can be thrown by statements in method’s body of by methods called in method’s body  Exceptions can be of types listed in throws clause or subclasses

14 Example: Handling ArithmeticExceptions and InputMismatchExceptions  With exception handling  program catches and handles the exception  Example, Figure 13.2 Figure 13.2Figure 13.2  Allows user to try again if invalid input is entered (zero for denominator, or non-integer input)

15 Sequence of Events for throw Preceding step try block throw statement unmatched catch matching catch unmatched catch next step

16 Sequence of Events for No throw Preceding step try block throw statement unmatched catch matching catch unmatched catch next step

17 When to Use Exception Handling  Exception handling designed to process synchronous errors  Synchronous errors – occur when a statement executes  Asynchronous errors – occur in parallel with and independent of the program’s flow of control  Avoid using exception handling as an alternate form of flow of control.

18 Java Exception Hierarchy  Superclass Throwable  Subclass Exception  Exceptional situations  Should be caught by program  Subclass Error  Typically not caught by program  Checked exceptions  Catch or declare  Unchecked exceptions

19 Java Exception Hierarchy  All exceptions inherit either directly or indirectly from class Exception  Exception classes form an inheritance hierarchy that can be extended

20 Java Exception Hierarchy  Class Throwable, superclass of Exception  Only Throwable objects can be used with the exception-handling mechanism  Has two subclasses: Exception and Error  Class Exception and its subclasses represent exception situations that can occur in a Java program and that can be caught by the application  Class Error and its subclasses represent abnormal situations that could happen in the JVM – it is usually not possible for a program to recover from Error s

21 Inheritance hierarchy for class Throwable

22 Checked Exceptions  Inherit from class Exception but not from RuntimeException  Compiler enforces catch-or-declare requirement  Compiler checks each method call and method declaration  determines whether method throws checked exceptions.  If so, the compiler ensures checked exception caught or declared in throws clause.  If not caught or declared, compiler error occurs.

23 Unchecked Exceptions  Inherit from class RuntimeException or class Error  Compiler does not check code to see if exception caught or declared  If an unchecked exception occurs and not caught  Program terminates or runs with unexpected results  Can typically be prevented by proper coding

24 Java Exception Hierarchy  catch block catches all exceptions of its type and subclasses of its type  If there are multiple catch blocks that match a particular exception type, only the first matching catch block executes  Makes sense to use a catch block of a superclass when all catch blocks for that class’s subclasses will perform same functionality

25 finally Block  Consists of finally keyword followed by a block of code enclosed in curly braces  Optional in a try statement  If present, is placed after the last catch block  View position, Figure 13.4 Figure 13.4Figure 13.4

26 finally Block  Executes whether or not an exception is thrown in the corresponding try block or any of its corresponding catch blocks  Will not execute if the application exits early from a try block via method System.exit  Typically contains resource-release code

27 Using finally  View program, Figure 13.5 Figure 13.5Figure 13.5  Note  Re-throw of exception  Code for throw exception  Blocks using finally  Suggestion  Do not use a try block for every individual statement which may cause a problem  Enclose groups of statements  Follow by multiple catch blocks

28 Sequence of Events for finally clause Preceding step try block throw statement unmatched catch matching catch unmatched catch next step finally