Jim Fawcett CSE687 – Object Oriented Design Spring 2015

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Exception Handling Outline 13.1 Introduction 13.2 Exception-Handling Overview 13.3 Other.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Exception Handling Handling an unexpected behaviour Unit - 08.
 2006 Pearson Education, Inc. All rights reserved. Exception Handling in C++ CS-2303, C-Term Exception Handling in C++ CS-2303 System Programming.
CSE 332: C++ exceptions Overview of C++ Exceptions Normal program control flow is halted –At the point where an exception is thrown The program call stack.
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.
Dale Roberts Exception Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Exception Handling: A Deeper.
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.
C++ Exception Handling
1 CSC241: Object Oriented Programming Lecture No 28.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
OOP Spring 2007 – Recitation 81 Object Oriented Programming Spring 2007 Recitation 8.
CSE333 SECTION 7. Template vs Generics C++ templates are similar to preprocessor macros A template will be “materialized” into multiple copies with T.
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.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
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.
COP4020 Programming Languages Exception Handling Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
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.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
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.
Exception Handling Outline 23.1 Introduction
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
CSCI 383 Object-Oriented Programming & Design Lecture 20 Martin van Bommel.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
CHAPTER 18 C – C++ Section 1: Exceptions. Error Handling with Exceptions Forces you to defend yourself Separates error handling code from the source.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Appendix H Exception Handling: A Deeper Look
C ++ MULTIPLE CHOICE QUESTION
Exception handling.
Exception Handling in C++
Exception Handling C++.
IS 0020 Program Design and Software Tools
Exceptions Error Handling and Recovery
16 Exception Handling.
Jim Fawcett CSE687-OnLine – Object Oriented Design Summer 2017
Exceptions Exceptions are used to signal that an unexpected event has happened in a program C++ will generate exceptions for some errors in the program.
CS212: Object Oriented Analysis and Design
CS 2704 Object Oriented Software Design and Construction
Jim Fawcett CSE687 – Object Oriented Design Spring 2001
Java Programming Fifth Edition
Java Programming Language
Why exception handling in C++?
EXCEPTION HANDLING.
Object Oriented Programming COP3330 / CGS5409
Chapter 14: Exception Handling
EE422C Software Implementation II
Exception Handling Chapter 9.
Exceptions with Functions
Chapter 17 Templates and Exceptions Part 2
Exception Handling Chapter 9 Edited by JJ.
Exception Handling In Text: Chapter 14.
Exceptions 2 CMSC 202.
Throwing and catching exceptions
Exception Handling.
Fundaments of Game Design
Department of Computer and Information Science, School of Science, IUPUI Exception Handling Dale Roberts, Lecturer Computer Science, IUPUI
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
CMSC 202 Exceptions.
Exception Handling.
Exceptions.
ENERGY 211 / CME 211 Lecture 24 November 14, 2008.
Presentation transcript:

Jim Fawcett CSE687 – Object Oriented Design Spring 2015 C++ Exceptions Jim Fawcett CSE687 – Object Oriented Design Spring 2015

How to Deal with Exceptional States? Ignore them: Wrong thing to do for all but demo programs. Abort processing – detect but don’t try to recover: Not appropriate for programs with safety issues or critical missions. Have functions return error codes: Program is constantly spending CPU cycles looking for rare events. Easy to miss a check. Use C++ Exceptions

C++ Exceptions Exceptions use three keywords: throw, try, and catch throw e: constructs an exception object, e, and takes it out of an enclosing context defined by a try block try {…}: defines, for thrown exceptions, an enclosing context with specified catch handlers catch(E e) {…}: exception handler catch(E e) responds to an exception object of type “E”

Example If no exception is thrown, the code in the try block is executed, the catch clause is skipped, and computation resumes after the catch clause. If the exception is thrown in some lower level scope, defined, perhaps, by a called function within the try block, all local objects in that scope are destroyed as the exception moves out of that scope. try { // some code that may throw an exception } Catch(exception &e) // some processing to attempt to recover from error // based on information carried by the exception If an exception is thrown somewhere in the try block, the remaining code in the try block is skipped, and a matching catch clause is entered, if found. Computation resumes after the last statement in matching catch clause. Matching is based on the type of the exception.

Chained Handlers Exception handlers are often chained at the end of a try block, e.g.: Matching attempts are based on the order of declaration of the handlers. try { // some code that may throw an exception } Catch(T1 t1) { // processing for type T1 Catch(T2 t2) { // processing for type T2

Cleaning Up C++ exception handling guarantees that as an exception leaves a scope all objects in that scope that have been successfully constructed will have their destructors called. Only destructors are called, so resources that are allocated in that scope but not deallocated by destructors will be lost. So, in an exception environment, you must make all allocations within objects and deallocate in their destructors. If a second exception is thrown while a first has not yet been handled, the special function terminate() is called (more on that later).

Matching A catch handler matches a thrown exception based on its type. If you throw a literal string, say: “big trouble in River City” then it can be caught with the catch handler: catch(char *msg) { … }. An exception handler that accepts a reference or pointer to a base class object will match a derived class object or pointer to a derived class object, respectively, as well as the base type specified. If a derived class object is passed to a handler by value it will be sliced to a base class object. If, however, a derived object is passed by reference or pointer, no slicing occurs, and polymorphic calls within the handler are honored. A catch handler with an ellipsis, catch(…) { … }, will catch any exception thrown in its context, not caught earlier.

Uncaught Exceptions If none of the catch handlers for a try block matches a thrown exception the exception moves to the next enclosing try block. If there is no match in any enclosing try block the exception is uncaught. An uncaught exception also occurs if a new exception is thrown before an existing one is handled. Cleanups may fail to occur with an uncaught exception, so this is an error. If an exception is uncaught the special function terminate() is called. Uncaught exceptions can always be avoided by enclosing the contents of main in a try block with an ellipsis handler.

terminate() Function Terminate is a function pointer with default value the C library function abort(). You can define your own terminate handler using set_terminate(void(*)()); Example: void Arnold() { std::cout << “I’ll be back” } int main() { set_terminate(Arnold); : }

Rethrowing Exceptions If your catch handler does not completely handle an exception you may re-throw it to the next enclosing context. catch(E e) { // processing to handle e is incomplete throw; } This allows processing an exception in several passes as it travels up through a series of try-contexts.

Standard Exceptions Standard Exception class: namespace std { class exception { public: virtual const char* what() const throw(); // create, copy, assign, and destroy // exception objects }; }

Standard Exceptions

Exception Specifications All exception specifications have been removed from C++11 except for throw() and nothrow(). A function can declare exception specifications: void f() throw (E1, E2, E3); declares that f may throw any of E1, E2, or E3. void f() throw() declares that no exceptions are thrown in f. void f() declares that any type exception may be thrown in f.

Specification Violations If an exception specification is violated, the special function unexpected() is called when the exception is thrown. By default unexpected() terminates execution. However, you may change that behavior by defining your own: void FreddyKrueger() { … } int main() { set_unexpected(FreddyKrueger); : }

Exception Safety (Sutter, 2000) Basic guarantee: In the presence of exceptions thrown by called global functions, object messages, template parameters, or library calls, the code: will not leak resources. will maintain in a consistent, if unpredictable, state.

Exception Safety Strong guarantee: If an operation terminates because of an exception, program state will remain unchanged. This implies commit-or-rollback semantics, including that no references or iterators will become invalid if an operation fails.

Exception Safety Nothrow guarantee: A function will not emit an exception under any circumstances. Strong exception safety isn’t possible unless certain functions are guaranteed not to throw.

Exception Safety To implement strong exception safety: In each function, take all the code that might emit an exception and do all its work safely off-to-the-side. Only when you know that work has succeeded should you modify program state, by swapping current state with the off-to-the-side state, using only non-throwing operations like pointer swaps. Destructors must always provide the nothrow guarantee, since destructors are called in the scope of an exception and a second active exception will always immediately call terminate() without further cleanup.

References The C++ Programming Language, 3rd Edition, Stroustrup, Addison-Wesley, 1997 Exceptional C++, Sutter, Addison- Wesley, 2000 There is a very nice summary in our text: The C++ Standard Library, Nicolai Josuttis, Addison Wesley, 1999