CS212: Object Oriented Analysis and Design Lecture 20: Exception Handling-II.

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.
1 Chapter 17-2 Templates and Exceptions Dale/Weems.
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 23 - Exception Handling Outline 23.1Introduction 23.2When Exception Handling Should Be Used 23.3Other.
Index Exception handling Exception In Java Exception Types
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.
SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions.
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.
Jerry Lebowitz. Topics  Provides a facility for a systematic object oriented approach to handling runtime errors ◦ Can also handle runtime errors.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
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 (Large parts of these copied from Ed Schonberg’s slides)
Concurrency - 1 Exceptions General mechanism for handling abnormal conditions Predefined exceptions: constraint violations, I/O errors, communication errors,
© Copyright Eliyahu Brutman Exceptions. © Copyright Eliyahu Brutman Exceptions and Design Patterns - 2 Introduction to Exception Handling Definition:
Rossella Lau Lecture 9, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 9: Application with Exception Handling 
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
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.
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.
SNU OOPSLA Lab. 14. Exception Handling © copyright 2001 SNU OOPSLA Lab.
1 Exception Handling Introduction to Exception Handling Exception Handling in PLs –Ada –C++ –Java Sebesta Chapter 14.
CPSC 252 Exception Handling Page 1 Exceptions and exception handling Client programmers can make errors using a class attempting to dequeue an item from.
Object Oriented Programming
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 13 - Exception Handling Outline 13.1Introduction 13.2When Exception Handling Should Be Used.
1 CSC241: Object Oriented Programming Lecture No 27.
COMPUTER PROGRAMMING 2 Exceptions. What are Exceptions? Unexpected events that happen when the code is executing (during runtime). Exceptions are types.
Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  Due to poor understanding of the problem and solution.
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
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.
© 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.
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.
C++ Namespaces, Exceptions CSci 588: Data Structures, Algorithms and Software Design All material not from online sources copyright © Travis Desell, 2011.
CSC241 Object-Oriented Programming (OOP) Lecture No. 22.
EE4E. C++ Programming Lecture 6 Advanced Topics. Contents Introduction Introduction Exception handling in C++ Exception handling in C++  An object oriented.
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.
Exception handling.
Java Exceptions a quick review….
C++ Exceptions.
IS 0020 Program Design and Software Tools
Exceptions Error Handling and Recovery
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
Chapter 17 Templates and Exceptions Part 2
Advanced C++ Exception Handling
Exception Handling.
Presentation transcript:

CS212: Object Oriented Analysis and Design Lecture 20: Exception Handling-II

Recap of Lecture 19 Exception handling Try, throw catch Stack unwinding Exception classes

Outline of Lecture 20 Derived class exception Special scenarios Exception Specifications Rethrow Terminate() and unexpected()

Exception handling Terminate the program (an exception isn’t caught) Return a value representing ‘‘error,’’ (isn’t always feasible) Return a legal value and leave the program in an illegal state, (calling function may not notice the error) Call a function supplied to be called in case of ‘‘error.’’ Exception  they are insufficient, inelegant, and error prone.

Types of try block A try block to indicate which areas in your program that might throw exceptions you want to handle immediately A function try block to indicate that you want to detect exceptions in the entire body of a function try { // statements } handlers { // statemnts } functionName try { // function body } handlers { // statemnts } Demonstration

Exception Specifications We can restrict the type of exceptions that a function can throw outside of itself Prevent a function from throwing any exceptions whatsoever Add a throw clause to a function definition Data types contained in the comma-separated type-list may be thrown ret-type func-name(arg-list) throw(type-list) { //... }

Restricting Exceptions Throwing any other type of expression will cause abnormal program termination Standard library function unexpected() By default, this causes abort() to be called Specify your own unexpected handler if you like Demonstration

Rethrowing an Exception A handler can’t completely handle the error. Rethrow an expression from within an exception handler Calling throw, by itself, with no exception This causes the current exception to be passed on to an outer try/catch sequence. Allow multiple handlers access to the exception

Re-Throw 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 ; // rethrow the exception } Demonstration

Handling Derived-Class Exceptions Need to be careful in ordering the catch statements Trying to catch exception types involving base and derived classes Want to catch exceptions of both a base class type and a derived class type Demonstration

Resource Management A resource: is allocated, used and properly released ‘‘Proper release’’ is achieved by having the function that acquired it, release it before returning to its caller void use_file (const char *fn ) { FILE * f = fopen (fn,"w "); // use f fclose (f ); }

“Resource acquisition is initialization” RAII: exception-safe resource management An object is not considered constructed until its constructor has completed Then and only then will stack unwinding call the destructor for the object. Don’t leave their objects in some ‘‘half-constructed’’ state Prevent “resource leak”

RAII Encapsulate each resource into a class, where The constructor acquires the resource and establishes all class invariants or throws an exception if that cannot be done The destructor releases the resource and never throws exceptions Always use the resource via an instance of a RAII-class that either Has automatic storage duration Is a non-static member of a class whose instance has automatic storage duration

terminate() During stack unwinding, a destructor throws an exception and that exception is not handled. The expression that is thrown also throws an exception, and that exception is not handled. The constructor or destructor of a nonlocal static object throws an exception, and the exception is not handled. A function registered with atexit() throws an exception, and the exception is not handled. The following demonstrates this:

unexpected() A function with an exception specification throws an exception The exception is not listed in its exception specification The unexpected() function is called The unexpected() function calls the function pointed to by unexpected_handler By default, unexpected_handler points to the function terminate()

Setting the Terminate and Unexpected Handlers These functions call other functions to actually handle an error terminate()  abort(); unexpected()  terminate() Allows the program to take full control of the exception handling subsystem set_terminate(): terminate handler set_unexpected(): unexpected handler Demonstration

uncaught_exception( ) Detects if the current thread has a live exception object An exception has been thrown or rethrown and not yet entered a matching catch clause uncaught_exception detects if stack unwinding is currently in progress Detects how many exceptions have been thrown or rethrown And not yet entered their matching catch clauses

Standard Exception Classes (SEC) Exception classes derived from logic_error Exception classes derived from runtime_error invalid_argumentInvalid argument out_of_rangeArgument value not in its expected range length_errorLength exceeds maximum capacity domain_errorDomain error reported by the implementation range_errorRange error in internal computation underflow_errorArithmetic underflow error overflow_errorArithmetic overflow error

Exception and constructors How to report errors from a constructor Constructor does not return a separate value for a caller to test Return an object in a bad state Set a nonlocal variable (e.g., errno) to indicate that the creation failed Don’t do any initialization in the constructor Mark the object ‘‘uninitialized’’

Different scenarios Exceptions and New What happens if X ´s constructor throws an exception? Resource Exhaustion Resumption, Termination Exceptions and Member Initialization A member initializer (directly or indirectly) throws an exception Exceptions in Destructors Normal call Call during exception handling

Exceptions That Are Not Errors If an exception is expected and caught so that it has no bad effects on the behaviour of the program, then how can it be an error? void f (Queue & q ){ try { for (;;) { X m = q.get(); // throws ‘Empty’ if queue is empty / /... } catch (Queue :: Empty) { return ; }

Thank you Next Lecture: Templates