CSCI 383 Object-Oriented Programming & Design Lecture 20 Martin van Bommel.

Slides:



Advertisements
Similar presentations
Topics Introduction Types of Errors Exceptions Exception Handling
Advertisements

Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer.
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.
Errors and Exceptions The objectives of this chapter are: To understand the exception handling mechanism defined in Java To explain the difference between.
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.
Review Linked list: Doubly linked list, insertback, insertbefore Remove Search.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 16 Exception Handling.
Index Exception handling Exception In Java Exception Types
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 1 CISC6795, Spring Introduction 2 Exception – an indication of a problem that occurs during a program’s execution, for examples:
Dale Roberts Exception Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
Error Handling with Exceptions Concepts C and other earlier languages often had multiple error-handling schemes, and these were generally established.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Lesson 16 Exceptions Lesson Exceptions1. Murphy’s Law Anything that can go wrong will go wrong Lesson Exceptions2.
1 CSC241: Object Oriented Programming Lecture No 28.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Exception Handling Introduction Exception handling is a mechanism to handle exceptions. Exceptions are error like situations. It is difficult to decide.
CSI 3120, Exception handling, page 1 Exception and Event Handling Credits Robert W. Sebesta, Concepts of Programming Languages, 8 th ed., 2007 Dr. Nathalie.
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.
© Copyright Eliyahu Brutman Exceptions. © Copyright Eliyahu Brutman Exceptions and Design Patterns - 2 Introduction to Exception Handling Definition:
Exception Handling An Exception is an indication of a problem that occurs during a program’s execution. Exception handling enables the programmer to create.
1 Exception and Event Handling (Based on:Concepts of Programming Languages, 8 th edition, by Robert W. Sebesta, 2007)
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
1 Exception Handling Introduction to Exception Handling Exception Handling in PLs –Ada –C++ –Java Sebesta Chapter 14.
June 14, 2001Exception Handling in Java1 Richard S. Huntrods June 14, 2001 University of Calgary.
1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about.
Object Oriented Programming
1 Exceptions (Section 8.5) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael Scott.
Chapter 12: Exception Handling
1 CSC241: Object Oriented Programming Lecture No 27.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Introduction to Exception Handling and Defensive Programming.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
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.
Exception Handling Programmers must deal with errors and exceptional situations: User input errors Device errors Empty disk space, no memory Component.
HANDLING EXCEPTIONS Chapter 9. Outline  Learn about the limitations of traditional error-handling methods  Throw exceptions  Use try blocks  Catch.
CS212: Object Oriented Analysis and Design Lecture 20: Exception Handling-II.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 23 - Exception Handling Outline 23.1Introduction.
Exception Handling Outline 23.1 Introduction
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
LECTURE LECTURE 14 Exception Handling Textbook p
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
EE4E. C++ Programming Lecture 6 Advanced Topics. Contents Introduction Introduction Exception handling in C++ Exception handling in C++  An object oriented.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Exception handling.
C++ Exceptions.
Exceptions Error Handling and Recovery
16 Exception Handling.
Chapter 16 Exception Handling
Jim Fawcett CSE687-OnLine – Object Oriented Design Summer 2017
EXCEPTION HANDLING IN C++
Jim Fawcett CSE687 – Object Oriented Design Spring 2001
Jim Fawcett CSE687 – Object Oriented Design Spring 2015
Why exception handling in C++?
Part IX Fundamentals of C and C++ Programming Exception Handling
Chapter 14: Exception Handling
Exception Handling Chapter 9 Edited by JJ.
Exception and Event Handling
CMSC 202 Exceptions.
Exception Handling.
Presentation transcript:

CSCI 383 Object-Oriented Programming & Design Lecture 20 Martin van Bommel

Fall 2010CSCI 383 Lecture 20 M. van Bommel 2 Exception Handling An exception is a run-time program exceptional event Exceptions include such things as Divide by zero Out of memory Arithmetic overflow Generally, there are several ways that one can handle an exception when it occurs, including Ignore the error Handle the error and terminate the program Handle the error and continue execution

Fall 2010CSCI 383 Lecture 20 M. van Bommel 3 How Should a Library Deal with Errors? Terminate the program Do you really want your library to kill your program? Print an error message and continue Would you allow a library to clobber your screen?

Fall 2010CSCI 383 Lecture 20 M. van Bommel 4 Exception Handling Exception handling is similar in concept to interrupt handling When an exception is raised, control branches to a separate function, called an exception handler, where the exception is processed Exception handlers may have a different scope than the function in which the exception occurs Thus, the language also must provide some mechanism for passing information from the routine where the exception occurs to the exception handler

Fall 2010CSCI 383 Lecture 20 M. van Bommel 5 Exception Handling In an ANSI C program, exception handling may look something like this char* p = (char*) malloc(sizeof(char) * size); if (p==NULL) printf(“Unable to allocate character array.\n”); exit(1); } This is analogous to the “good ol’ days” of programming when data and functions coexisted on a casual basis Such code is usually intermingled with the conventional processing code of the program. This can make the processing code difficult to read, understand, and maintain

Fall 2010CSCI 383 Lecture 20 M. van Bommel 6 Exception Handling C++ provides constructs to support exception handling more directly Exceptions can be raised by programmer’s code, by the system, or by library code Although using exception handling can simplify a C++ program, it also can make the program even more complex than it was originally

Fall 2010CSCI 383 Lecture 20 M. van Bommel 7 Exception Handling C++ provides three new constructs for exception handling. They are try blocks throw statements catch blocks A try block is a block of statements in which exceptions are to be detected A throw statement throws or raises an exception A catch block is an exception handler Handout #8 contains a simple program that illustrates the use of exception handling in C++ Handout #8

Fall 2010CSCI 383 Lecture 20 M. van Bommel 8 Exception Handling: throw The throw statement usually appears with one argument, which is an exception object to be thrown The exception object can be of a built-in or user- defined type Sometimes, the exception object is empty. This might occur if the type of the object is all the information the exception handler needs to do its work For example, one might create an exception object class such as: class DivideByZero{};

Fall 2010CSCI 383 Lecture 20 M. van Bommel 9 Exception Handling: throw The C++ standard library contains some pre-defined exception object classes for commonly thrown exceptions If an object is thrown, it makes sense that it must be caught by something. It generally is caught by a catch block

Fall 2010CSCI 383 Lecture 20 M. van Bommel 10 Exception Handling: try A try block is the keyword try statement followed by a block of statements in which exceptions are detected A try block always is followed by a sequence of one or more catch blocks When an exception is raised while executing the code contained in a try block, the exception object is thrown to the closest exception handler that specifies the appropriate type of exception object If none of the catch blocks specifies an appropriate type of exception object, the object is thrown out to the next level of try block within which execution is taking place

Fall 2010CSCI 383 Lecture 20 M. van Bommel 11 Exception Handling: try If the exception makes it all the way to the global level without being handled, it triggers a call to a built-in function named terminate, which aborts the program execution Note that the throw statements in Handout #8 are in methods that are not implemented within a try block.Handout #8 Does this mean that terminate will be called when the exceptions are thrown? Not necessarily!

Fall 2010CSCI 383 Lecture 20 M. van Bommel 12 Exception Handling: try Though the point of execution currently may be outside the scope of the try block, C++ begins to do a stack unwind when the exception is thrown If the stack is entirely unwound and no appropriate exception handler is found, then terminate is called

Fall 2010CSCI 383 Lecture 20 M. van Bommel 13 Exception Handling: catch Each catch block begins with the keyword catch, followed by a single parenthesized parameter (in most cases) and a block of statements When an exception is raised within the try block, the catch blocks are examined – in the order in which they appear – in an attempt to find an appropriate exception handler

Fall 2010CSCI 383 Lecture 20 M. van Bommel 14 Exception Handling: catch The criteria used to select an appropriate exception handler are based on comparing the type of the exception object to the type of the parameter If the class of the catch parameter matches exactly the class of the exception object, the catch block is selected as appropriate If the class of the catch parameter is a public base class of the exception class object, the catch block is selected as appropriate If the catch parameter is of a pointer (or reference) type to which the pointer (or reference) type of the exception object can be converted, the catch block is selected as appropriate If the catch parameter is (...), the catch block is selected as appropriate. This is the “catch all”

Fall 2010CSCI 383 Lecture 20 M. van Bommel 15 Exception Handling: catch One must exercise care when ordering the catch blocks following a try block Here is an incorrect example: it assumes that ArrayErr is the public base class of RangeErr catch(...) {...} catch(ArrayErr&) {...} catch(RangeErr&) {...} What would be a correct ordering?

Fall 2010CSCI 383 Lecture 20 M. van Bommel 16 Exception Handling: catch The parenthesized parameter can consist of just a type followed by an identifier name, or simply a type. It can also consist only of ellipsis When an exception handler is chosen and has finished executing, execution continues with the statement following the last handler in the sequence of handlers associated with that try block

Fall 2010CSCI 383 Lecture 20 M. van Bommel 17 Exception Handling: catch A handler can re-throw an exception (and the exception object) if it decides that it cannot handle the exception or if it has finished processing the exception and wants to pass it along for further processing This is done by including the statement throw; in the handler

Fall 2010CSCI 383 Lecture 20 M. van Bommel 18 Standard Library Exception Hierarchy Experience has shown that exceptions fall nicely into a number of categories The C++ standard library includes a hierarchy of exception classes exception runtime_errorlogic_error overflow_errorunderflow_errorinvalid_argumentlength_errorout_of_range bad_allocbac_castbad_type_idbad_exception

Fall 2010CSCI 383 Lecture 20 M. van Bommel 19 Standard Library Exception Hierarchy This hierarchy is headed by base class exception, which contains virtual function what, which derived classes can override to issue appropriate error messages Handout #9 Notice that the catch block in Handout #9 could also have beenHandout #9 catch(runtime_error& runtimeException)

Fall 2010CSCI 383 Lecture 20 M. van Bommel 20 Throw List One can restrict the types of exceptions that can be thrown in a function by specifying a throw list as part of the function prototype. For example void myFunction(void) throw(RangeErr,DivideByZero) {...} If one throws a different type of exception than those in the list, the system function unexpected is called, which (by default) calls terminate An empty throw list (i.e., throw() ) specifies that any exceptions thrown by the function will trigger a call to unexpected Handout #10