SNU OOPSLA Lab. 14. Exception Handling © copyright 2001 SNU OOPSLA Lab.

Slides:



Advertisements
Similar presentations
L7: Exceptions Problem Description Error Handling –terminate the program –return a value representing an error –return a legal value and leave the program.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
 2006 Pearson Education, Inc. All rights reserved. Exception Handling in C++ CS-2303, C-Term Exception Handling in C++ CS-2303 System Programming.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 23 - Exception Handling Outline 23.1Introduction 23.2When Exception Handling Should Be Used 23.3Other.
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.
CSIS 123A Lecture 11 Exception Handling. Introduction  Typical approach to development:  Write programs assuming things go as planned  Get ‘core’ working.
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.
Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
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.
Jerry Lebowitz. Topics  Provides a facility for a systematic object oriented approach to handling runtime errors ◦ Can also handle runtime errors.
C++ Exception Handling
1 CSC241: Object Oriented Programming Lecture No 28.
Exception Handling Introduction Exception handling is a mechanism to handle exceptions. Exceptions are error like situations. It is difficult to decide.
CS Advanced C++ Exception Handling Topic #5.
. Plab – Tirgul 10 Exceptions. Error handling in C Up to now we handled our errors in the “C way”: assert return codes global error variable ( errno and.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Concurrency - 1 Exceptions General mechanism for handling abnormal conditions Predefined exceptions: constraint violations, I/O errors, communication errors,
Plab – Tirgul 8 Exceptions. Error handling in C Up to now we handled our errors in the “C way”: assert return codes global error variable ( errno and.
OOP Spring 2007 – Recitation 81 Object Oriented Programming Spring 2007 Recitation 8.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
Exceptions Objectives At the conclusion of this lesson, students should be able to Explain the need for exceptions Correctly write programs that use.
 2006 Pearson Education, Inc. All rights reserved Exception Handling.
1 Exception and Event Handling (Based on:Concepts of Programming Languages, 8 th edition, by Robert W. Sebesta, 2007)
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Object Oriented Programming
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 13 - Exception Handling Outline 13.1Introduction 13.2When Exception Handling Should Be Used.
C++ Exceptions STL Vector. Example int Quotient (int numer, int denom} { if (denom != 0) return (numer/denom); else //What to do?? }
Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  Due to poor understanding of the problem and solution.
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 –
Exception Handling Programmers must deal with errors and exceptional situations: User input errors Device errors Empty disk space, no memory Component.
CS212: Object Oriented Analysis and Design Lecture 20: Exception Handling-II.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 4 is due Nov. 20 (next Friday). After today you should know everything you need for assignment.
Exceptions and Program Correctness based on the original work by Dr. Roger deBry Version 1.1.
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 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.
1 Becoming More Effective with C++ … Day Two Stanley B. Lippman
CSCI 383 Object-Oriented Programming & Design Lecture 20 Martin van Bommel.
LECTURE LECTURE 14 Exception Handling Textbook p
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
CSC241 Object-Oriented Programming (OOP) Lecture No. 22.
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.
1 Lecture Material Exceptions. 2 Grouping of Exceptions Often, exceptions fall naturally into families. This implies that inheritance can be useful to.
Exceptions handling Try, catch blocks Throwing exceptions.
Exception handling.
Exception Handling in C++
IS 0020 Program Design and Software Tools
Exceptions Error Handling and Recovery
Chapter 16 Exception Handling: A Deeper Look
Jim Fawcett CSE687-OnLine – Object Oriented Design Summer 2017
CS212: Object Oriented Analysis and Design
CMSC202 Computer Science II for Majors Lecture 16 – Exceptions
Jim Fawcett CSE687 – Object Oriented Design Spring 2001
Jim Fawcett CSE687 – Object Oriented Design Spring 2015
Object-Oriented Programming (OOP) Lecture No. 45
Why exception handling in C++?
Chapter 14: Exception Handling
Exceptions handling Try, catch blocks Throwing exceptions.
16 Exception Handling.
CMSC 202 Exceptions.
Exception Handling.
Presentation transcript:

SNU OOPSLA Lab. 14. Exception Handling © copyright 2001 SNU OOPSLA Lab.

SNU OOPSLA Lab. C++ 2 Contents Error Handling Grouping of Exceptions Catching Exceptions Resource Management Exceptions That Are Not Errors Exception Specifications Uncaught Exceptions Standard Exceptions

SNU OOPSLA Lab. C Error Handling(1) Author of a library User of a library Can detect run-time errors but does not in general have any idea what to do about them Problem occurs May know how to cope with such errors but cannot detect them Notion of Exception

SNU OOPSLA Lab. C Error Handling(2) Traditional techniques terminate the program return a value representing “error” return a legal value and leave the program in an illegal state call a function supplied to be called in case of “error”

SNU OOPSLA Lab. C++ 5 Exception handling separates error handling code from “ordinary code”, so program is more readable more amenable In exception handling, default response to an error is to terminate the program, so design process is more important the work involved in getting a program running is harder 1. Error Handling(3)

SNU OOPSLA Lab. C++ 6 Alternative views on exceptions to support error handling to handle only synchronous exceptions ex) array range checks and I/O errors a non-local control structure based on stack C++ exception handling mechanisms are provided to report and handle errors and exceptional events 1.1 Alternative views on exceptions

SNU OOPSLA Lab. C++ 7 Structure of exception handling class exception_type {... }; try { throw exception_object ; // throw exception } catch ( exception_type ) { // exception handler //... } Structure of exception handling

SNU OOPSLA Lab. C++ 8 An exception is an object of some class representing an exceptional occurrence Code that detects an error throws an object A piece of code express desire to handle an exception by a catch clause The effect of throw is to unwind the stack until catch is found Exceptions fall into families 2. Grouping of Exceptions (1)

SNU OOPSLA Lab. C Grouping of Exceptions (2) Exception for a mathematical library Handle any Matherr without caring precisely which kind it is class Matherr { }; class Overflow : public Matherr{ }; class Underflow : public Matherr{ }; class Zerodivide : public Matherr{ }; try { //... } catch (Overflow) { // handle Overflow or anything derived from Overflow } catch (Matherr) { // handle any Matherr that is not Overflow }

SNU OOPSLA Lab. C++ 10 Organizing into hierarchies can be important for robustness of code If not, be done by exhaustively listing the exceptions 2. Grouping of Exceptions (3) Matherr Underflow ZerodivideOverflow Without grouping void g() { try { // … } catch (Overflow) { /* … */ } catch (Underflow) { /* … */ } catch(Zerodivide) { /* … */ } }

SNU OOPSLA Lab. C Derived Exceptions(1) An exception is typically caught by a handler for its base class rather than by a handler for its exact class class Matherr { // … virtual void debug_ptint() const { … } }; class Int _overflow : public Matherr { const char* op; int a1, a2; public: Int_overflow(const char *p, int a, int b) { … } virtual void debug_print() const { … } // … }; void f() { try { g(); } catch (Matherr m) { // … } If g() throw Int_overflow?  M is Matherr object(Problem)

SNU OOPSLA Lab. C Derived Exceptions(2) Int add(int s, int y) { if ((x>0 && y>0 && x>INT_MAX-y) || x<0 && y<0 && x<INT_MIN-y)) throw Int_overflow(“+”,x,y); return x+y; } void f() { try { int i1 = add(1, 2); int i2 = add(INT_MAX, 2); } catch (Matherr& m) { m.debug_print(); } Int_overflow::debug_print() will be invoked

SNU OOPSLA Lab. C Composite Exceptions Not every grouping of exceptions is a tree structure Often, an exception belongs to two groups class Netfile_err : public Network_err, public File_system_err { …}; void f() { try { // something catch (Network_err& e) { // … } void g() { try { // something else } catch (File_system_err& e) { // … } Network exception File system exception

SNU OOPSLA Lab. C Catching Exceptions(1) The handler is invoked: À if H is the same type as E Á if H is an unambiguous public base of E  if H and E are pointer types and  or  holds for the types to which they refer à if H is a reference and  or  holds for the type to which H refers void f() { try { throw E(); } catch(H) { // when do we get here? }

SNU OOPSLA Lab. C++ 15 An exception is copied when it is thrown, so the handler gets hold of a copy of the original exception An exception may be copied several times before it is caught Cannot throw an exception that cannot be copied 3. Catching Exceptions(2)

SNU OOPSLA Lab. C Re-Throw The handler typically does what can be done locally and then throws the exception again void h() { try { // code that might throw Math errors } catch (Matherr) { if (can_handle_it_completely) { // handle the Matherr return; } else { // do what can be done here throw;// re-throw the exception } without operand

SNU OOPSLA Lab. C Catch Every Exception Catch any exception  catch(…) void m() { try { // something } catch(...) {//handle every exception // cleanup throw; }

SNU OOPSLA Lab. C Order of handlers The handlers are tried in order void f() { try { // … } catch (std::ios_base::failure) { // handle any stream io error } catch (std::exception& e) { // handle any standard library exception } catch (…) { //handle any other exception }  

SNU OOPSLA Lab. C Resource Management(1) The function that acquired a resource release it before returning to its caller void use_file (const char* fn) { FILE* f=open(fn, “r”); // use f fclose(f); } void use_file (const char* fn) { FILE* f=open(fn, “r”); try { // use f } catch (…) { fclose(f); throw; } fclose(f); } an exception may cause use_file() to be exited without fcolse() being called Ver. 1

SNU OOPSLA Lab. C++ 20 Problem with Ver. 1 is that it is verbose, tedious, and potentially expensive 4. Resource Management(2) void acquire() { // acquire resource 1 // … // acquire resource n // use resources // release resource n // … // release resource 1 } General form of the problem(Ver. 1) reverse order Using constructors and destructors class File_ptr { FILE* p; public: File_ptr (const char* a) { p = fopen (n, a); } File_ptr (FILE* pp) { p = pp; } ~File_ptr() { fclose(p); } operator FILE* () { return p; } }; void use_file (const char* fn) { File_ptr f(fn, “r”); // use f } Ver. 2

SNU OOPSLA Lab. C Using constructors and destructors(1) “Resource acquisition is initialization” - the technique for managing resources using local objects An object is not considered constructed until its constructor has completed

SNU OOPSLA Lab. C Using constructors and destructors(2) Memory acquisition class Y { int* p; void init(); public: Y(int s) { p = new int[s]; init(); } ~Y() { delete []p; } // … }; If an exception is thrown by init(), then the store acquired will not be freed The destructor will not be called because the object wasn’t completely constructed class Z { vector p; void init(); public: Z(int s) : p(s) { init(); } // … }; The memory used by p is managed by vector

SNU OOPSLA Lab. C Auto_ptr(1) The standard library provides the template class auto_ptr Supports the “resource acquisition is initialization” technique class Mess { }; void f(Point p1, Point p2) { auto_ptr p(new Rectangle(p1, p2)); // p points to the rectangle p->retate(45); // … if (in_a_mess) throw Mess(); // … } Rectangle is deleted whether or not an exception is thrown

SNU OOPSLA Lab. C++ 24 auto_ptr simplifies the task of writing classes with members that are pointers to objects that need to be deleted when the class objects is destroyed 4.2 Auto_ptr(2) Class X { auto_ptr p; // … public: X() { p = new Y(2); /* … */ }; // … };

SNU OOPSLA Lab. C++ 25 template class std::auto_ptr { public: typedef X element_type; explicit auto_ptr(X* =0) throw(); auto_ptr(const auto_ptr&) throw(); template &) throw(); auto_ptr& operator=(const auto_ptr&) throw(); template auto_ptr& operator=(const auto_ptr &) throw(); ~auto_ptr(); X& operator*() const throw(); X* operaotr->() const throw(); X* relaease() const throw();// relinquish ownership X* get() const throw();//get ownership back // … implementation details … }; standard header

SNU OOPSLA Lab. C++ 26 After initial construction, an auto_ptr “owns” the object to which it holds a pointer; it is responsible for deleting it The effect of having more than one auto_ptr “own” an object is undefined; most likely the object will be deleted twice 4.2 Auto_ptr(3) void g(X* p) { auto_ptr p2(p);// now p2 is responsible for deletion auto_ptr p3(p2);// now p3 is responsible for deletion (and p2 isn’t) auto_ptr p4(p);// programming error: // now p4 is also responsible for deletion // … }

SNU OOPSLA Lab. C Caveat Not all programs need to be resilient against all forms of failure, and not all resources are critical enough to warrant the effort to protect then using “resource acquisition is initialization,” auto_ptr, and catch(…)

SNU OOPSLA Lab. C Exceptions and New What happens if X’s constructor throws an exception? Void f(Arena& a, X* buffer) { X* p1 = new X; X* p2 = new X[10]; X* p3 = new (buffer[10]) X;// place X in buffer (no deallocation needed) X* p4 = new (buffer[11]) X[10]; X* p5 = new(a) X;// allocation from Arena a (deallocate from a) X* p6 = new(a) X[10]; }

SNU OOPSLA Lab. C Resource Exhaustion(1) What to do when an attempt to acquire a resource fails Two styles of solutions resumption : ask some caller to fix the problem and carry on termination : abandon the computation and return to some caller In C++, resumption model : function-call mechanism termination model : exception handling model

SNU OOPSLA Lab. C Resource Exhaustion(2) void* operator new(size_t size) { for (;;) { if (void* p = malloc(size)) return p;// try to find memory if (_new_handler == 0) throw bad_alloc();// no handler: give up _new_handler();// ask for help } void my_new_handler() { int no_of_bytes_found = find_some_memory(); if (no_of_bytes_found < min_allocation) throw bad_alloc();//give up Set_new_handler(&my_new_handler); Pointer to a function maintained by the standard function set_new_handler

SNU OOPSLA Lab. C Exceptions in Constructors(2) Exception handling allows the information that a construction failed to be transmitted out of the constructor class Vector { public: class Size { }; enum { max = }; Vector::Vector(int sz) { if (sz<0 || max<sz) throw Size(); //... } // … }; Protect itself from excessive demands on memory

SNU OOPSLA Lab. C Exceptions in Constructors(2) Vector* f(int i) { try { Vector* p = new Vector(i); // … return p; } catch (Vector::Size) { // deal with size error }

SNU OOPSLA Lab. C Exceptions and Member Initialization(1) What happens if a member initializer throws an exception? By default, the exception is passed on to whatever invoked the constructor for the member’s class The constructor itself can catch such exceptions by enclosing the complete function body in a try-block

SNU OOPSLA Lab. C Exceptions and Member Initialization(2) class X { Vector v; // … public: X(int); // … }; X::X(int s) try :v(s)// initialize v by s { // … } catch (Vector::Rectagle) { // exceptions thrown for v are caught here // … }

SNU OOPSLA Lab. C Exceptions in Destructors A destructor can be called in one of two ways normal call : as the result of a normal exit from a scope, a delete, etc. call during exception handling : during stack unwinding, the exception-handling mechanism exits a scope containing an object with a destructor If a destructor calls functions that may throw exceptions, it can protect itself X::~X() try { f();// might throw } catch (…) { // do something }

SNU OOPSLA Lab. C Exception Specifications Specify the set of exceptions that might be thrown as part of the function declaration void f(int a) throw (x2, x3); f() may throw only exceptions x2, x3, and exceptions derived from these types void f() throw (x2, x3) { // stuff } void f() try { // stuff } catch (x2) { throw; } catch (x3) {throw; } catch (…) { std::unexpected(); } = The most important advantage is that the function declaration belongs to an interface that is visible to its callers

SNU OOPSLA Lab. C Checking Exception Specifications(1) If any declaration of a function has an exception- specification, every declaration of that function must have an exception-specification with exactly the same set of exception types int f() throw (std::bad_alloc); int f() { // … } error : exception-specification missing

SNU OOPSLA Lab. C++ 38 A virtual function may be overridden only by a function that has an exception-specification at least as restrictive as its own exception-specification 6.1 Checking Exception Specifications(2) Class B { public: virtual void f();// can throw anything virtual void g() throw (X, Y); virtual void h() throw (X); }; class D : public B { public: void f() throw (X);//ok void g() throw (X);//ok void h() throw (X, Y); }; error: D::h() is less restrictive than B::h()

SNU OOPSLA Lab. C Unexpected Exceptions An exception-specification can lead to calls to unexpected() Such calls can be avoided through careful organization of exceptions and specification of interfaces Calls to unexpected() can be intercepted and rendered harmless

SNU OOPSLA Lab. C Mapping Exceptions The policy of terminating a program upon encountering an unexpected exception is too Draconian The behavior of unexpected() must be modified Add the standard library exception std::bad_exception to an exception-specification Class X{ }; class Y{ }; void f() throw (X, std::bad_exception) { // … throw Y(); } The exception-specification will catch the unacceptable exception Y and throw an exception of type bad_exception instead

SNU OOPSLA Lab. C Uncaught Exceptions If an exception is thrown but not caught, the function std::terminate() will be called The response to an uncaught exception is determined by an _uncaught_handler set by set_terminate() Typedef void(*PFV)(); PFV set_terminate(PFV);

10. Standard Exceptions(1) Standard Exceptions (thrown by language) NameThrown byReference bad_allocnew§ , § bad_castdynamic_cast§ bad_typeidtypeid§ bad_exceptionexception specification§ Standard Exceptions (thrown by standard library) out_of_range at()§3.7.2, §16.3.3, § bitset<>::operator[]()§ invalid_argument bitset constructor§ overflow_error bitset<>::to_ulong()§ ios_base::failure ios_base::clear§21.3.6

SNU OOPSLA Lab. C Standard Exceptions(2) exception logic_errorruntime_error length_error domain_error out_of_range invalid_argument bad_alloc bad_exception ios_base::failure bad_cast range_error overflow_error underflow_errorbad_typeid