Department of Computer and Information Science, School of Science, IUPUI Exception Handling Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu.

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. Introduction Errors can be dealt with at place error occurs –Easy to see if proper error checking implemented –Harder to read application.
Exception Handling Introduction Try Throw Catch. Exception Handling Use exception handling to write programs that are –Clearer –More robust –More fault-tolerant.
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 23 - Exception Handling Outline 23.1Introduction 23.2When Exception Handling Should Be Used 23.3Other.
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.
Dale Roberts Exception Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
 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.
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.
CS Advanced C++ Exception Handling Topic #5.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 14 - Exception Handling Outline 14.1Introduction 14.2When Exception Handling Should Be Used 14.3Other.
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.
Exceptions COMPSCI 105 S Principles of Computer Science.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Dale Roberts Exception Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 13 - Exception Handling Outline 13.1Introduction 13.2When Exception Handling Should Be Used.
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 –
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.
Exception Handling Programmers must deal with errors and exceptional situations: User input errors Device errors Empty disk space, no memory Component.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 14 - Exception Handling Outline 14.1Introduction 14.2When Exception Handling Should Be Used 14.3Other.
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
CSCI 383 Object-Oriented Programming & Design Lecture 20 Martin van Bommel.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
Exception Handling How to handle the runtime errors.
EE4E. C++ Programming Lecture 6 Advanced Topics. Contents Introduction Introduction Exception handling in C++ Exception handling in C++  An object oriented.
Namespace יום ראשון 13 נובמבר 2016 יום ראשון 13 נובמבר 2016 יום ראשון 13 נובמבר 2016 יום ראשון 13 נובמבר 2016 יום ראשון 13 נובמבר 2016 יום ראשון 13 נובמבר.
Exception handling.
Exception Handling in C++
C++ Exceptions.
IS 0020 Program Design and Software Tools
Exceptions Error Handling and Recovery
16 Exception Handling.
Chapter 16 Exception Handling: A Deeper Look
Chapter 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
EXCEPTION HANDLING IN C++
Chapter 14 – Exception Handling
Jim Fawcett CSE687 – Object Oriented Design Spring 2001
Exception Handling: A Deeper Look
Andy Wang Object Oriented Programming in C++ COP 3330
Jim Fawcett CSE687 – Object Oriented Design Spring 2015
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
Why exception handling in C++?
Part IX Fundamentals of C and C++ Programming Exception Handling
EXCEPTION HANDLING.
Object Oriented Programming COP3330 / CGS5409
Chapter 14: Exception Handling
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 17 Templates and Exceptions Part 2
Advanced C++ Exception Handling
Exceptions 1 CMSC 202.
Lecture 11 Objectives Learn what an exception is.
7 Arrays.
Object-Oriented Programming (OOP) Lecture No. 43
CMSC 202 Exceptions.
Java Programming: From Problem Analysis to Program Design, 4e
ENERGY 211 / CME 211 Lecture 24 November 14, 2008.
CMSC 202 Lesson 20 Exceptions 1.
Presentation transcript:

Department of Computer and Information Science, School of Science, IUPUI Exception Handling Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu

Purpose of Exception Handling Deal with errors in an elegant manner Write clearer, most robust, and fault tolerant program Error handling code Minimum --- Casual Systems Extensive --- Commercial products

Dealing with Errors Errors are interspersed throughout the software. Errors are handled where they occur. Programmer can see the error processing near to the code and determine the appropriateness of the error processing code. Code becomes "polluted" with error processing. More difficult for a programmer concerned with the application itself to read the code and determine if the code is functioning completely. User exception handling techniques Remove error code from the "main code" better readability and modifiability Exception Handling – remove the error handling code from the “main-line” of the program’s execution – improves program readability and maintainability – catch all kinds of exceptions, or a subset of them – programs become more robust Memory allocation, Arithmetic Exceptions, Invalid Parameters, Array Bounds, etc. – synchronous errors.

Dealing with Errors Used in situations where the system can recover from the error causing the exception – the recovery procedures is called the exception handler. Exception handling is typically used when the error will be dealt with by a different part of the program (i.e. a different scope) from where the error was detected. Particularly useful for graceful degradation. Use only to process exceptional situations.

Dealing with Errors To process exceptions for program components that are not geared to handle those exceptions directly. To process exception from software components, such as functions, libraries, classes, etc., that are likely to be widely used and where it does not make sense for those components to handle their own exceptions. To handle error processing in a uniform manner across large-scale projects. Use assert macro - if an assertion is false, program terminates - useful at debugging

Dealing with Errors Ignore exceptions! Abort program - what if resources were allocated to a program and it aborted? ("Resource Leak") Set and test error indicators - need to check them at all points in the program

Error Handling -- Method to Cope with Uncertainties A Function which Finds that it Cannot Cope With a Problem Throws an Exception, Hoping that Caller can Handle that Problem A Function that Wants to Handle that Kind of a Problem can Indicate that it is Willing to Catch that Exception Method of Transferring Control and Information to an Associated Exception Handler A try Block Constitutes the Section of the Program that is Subject to Exception Checking A Handler is Invoked with a throw Expression from within the try Block The Handler is the catch Function

Error Handling -- Method to Cope with Uncertainties (cont)

Invoking of Handler The Type of the Object Argument of the throw Expression Determines the Appropriate catch Invocation A throw Expression Without an Argument Re-throws the Exception Being Handled Again. Such a throw Expression MAY APPEAR ONLY IN A HANDLER or in a Function Directly Called From the Handler The catch (...) Handler, if Present, CAN Handle ALL Exception Classes

Invoking of Handler If NO Appropriate Handler is Present, the unexpected() Function is Invoked. It Passes the Control to terminate() Function and can Cause Termination The throw Expression is First Passed to the Appropriate Constructor for the Class -- A Default Constructor MAY be Created, if Needed -- No Constructor is Used for Non-Classes. The Processing is then Passed to the Handler

catch Handlers The catch Handler MUST IMMEDIATELY FOLLOW the Appropriate try Block or Other catch Handlers The Placement Order is Significant. Control is Passed to the FIRST HANDLER THAT CAN HANDLE THE CONDITION The catch(...) Handler, if Present, SHOULD BE THE LAST HANDLER

Simple Exception Handler -- Example #include <iostream> main(){ //try Block try {throw(long(1)); //catch(long) call} //Handlers catch (int x) {cout << "Catch Integer " << x << endl;} catch (long x) {cout << "Catch Long " << x << endl;} catch (...) {cout << "Catch Rest" << endl;} } OUTPUT WILL BE: ------ ---- -- Catch Long 1 Earlier g++ compiler verions required the usae of the =fhandle-exceptions or –fexceptions flags. The current g++ compiler support exception handling by default.

An Exception Handler Class -- Example #include<stream.h> #include<stdlib.h> //exit() class zero{ public: zero() //Constructor {cout<<"Class Zero Constructor Invoked!!"<<endl;} }; //Exception Checker Function void zero_check(int i){ if (i == 0) throw zero(); //Argument is of zero class type.} main(){ //try block try{for (int i = 2; ; i--){ zero_check(i); cout << "Reciprocal: " << 1.0/i << endl;}} //Handler catch(zero) {cout << "DIVIDE BY ZERO -- ERROR!!\n"; exit(-1);} } OUTPUT WILL BE: ------ ---- --- Reciprocal: 0.5 Reciprocal: 1 Class Zero Constructor Invoked!! DIVIDE BY ZERO -- ERROR!!

1. Class definition 1.1 Function definition 1 // Fig. 23.1: fig23_01.cpp 2 // A simple exception handling example. 3 // Checking for a divide-by-zero exception. 4 #include <iostream> 5 6 using std::cout; 7 using std::cin; 8 using std::endl; 9 10 // Class DivideByZeroException to be used in exception 11 // handling for throwing an exception on a division by zero. 12 class DivideByZeroException { 13 public: 14 DivideByZeroException() 15 : message( "attempted to divide by zero" ) { } 16 const char *what() const { return message; } 17 private: 18 const char *message; 19 }; 20 21 // Definition of function quotient. Demonstrates throwing 22 // an exception when a divide-by-zero exception is encountered. 23 double quotient( int numerator, int denominator ) 24 { 25 if ( denominator == 0 ) 26 throw DivideByZeroException(); 27 28 return static_cast< double > ( numerator ) / denominator; 29 } 1. Class definition 1.1 Function definition

1.2 Initialize variables 2. Input data 2.1 try and catch blocks 30 31 // Driver program 32 int main() 33 { 34 int number1, number2; 35 double result; 36 37 cout << "Enter two integers (end-of-file to end): "; 38 39 while ( cin >> number1 >> number2 ) { 40 41 // the try block wraps the code that may throw an 42 // exception and the code that should not execute 43 // if an exception occurs 44 try { 45 result = quotient( number1, number2 ); 46 cout << "The quotient is: " << result << endl; 47 } 48 catch ( DivideByZeroException ex ) { // exception handler 49 cout << "Exception occurred: " << ex.what() << '\n'; 50 } 51 52 cout << "\nEnter two integers (end-of-file to end): "; 53 } 54 55 cout << endl; 56 return 0; // terminate normally 57 } 1.2 Initialize variables 2. Input data 2.1 try and catch blocks 2.2 Function call 3. Output result

Program Output Enter two integers (end-of-file to end): 100 7 The quotient is: 14.2857 Enter two integers (end-of-file to end): 100 0 Exception occurred: attempted to divide by zero Enter two integers (end-of-file to end): 33 9 The quotient is: 3.66667 Enter two integers (end-of-file to end): Program Output

Throwing an Exception throw - indicates an exception has occurred Usually has one operand (sometimes zero) of any type If operand an object, called an exception object Conditional expression can be thrown Code referenced in a try block can throw an exception Exception caught by closest exception handler Control exits current try block and goes to catch handler (if it exists) Example (inside function definition) if ( denominator == 0 ) throw DivideByZeroException(); Throws a dividebyzeroexception object Exception not required to terminate program However, terminates block where exception occurred

Catching an Exception Exception handlers are in catch blocks Example: Format: catch( exceptionType parameterName){ exception handling code } Caught if argument type matches throw type If not caught then terminate called which (by default) calls abort Example: catch ( DivideByZeroException ex) { cout << "Exception occurred: " << ex.what() <<'\n' Catches exceptions of type DivideByZeroException

Catching an Exception (cont) Catch all exceptions catch(...) - catches all exceptions You do not know what type of exception occurred There is no parameter name - cannot reference the object If no handler matches thrown object Searches next enclosing try block If none found, terminate called If found, control resumes after last catch block If several handlers match thrown object, first one found is executed

Catching an Exception (cont) catch parameter matches thrown object when They are of the same type Exact match required - no promotions/conversions allowed The catch parameter is a public base class of the thrown object The catch parameter is a base-class pointer/ reference type and the thrown object is a derived-class pointer/ reference type The catch handler is catch( ... ) Thrown const objects have const in the parameter type

Catching an Exception (cont) Unreleased resources Resources may have been allocated when exception thrown catch handler should delete space allocated by new and close any opened files catch handlers can throw exceptions Exceptions can only be processed by outer try blocks

Rethrowing an Exception Rethrowing exceptions Used when an exception handler cannot process an exception Rethrow exception with the statement: throw; No arguments If no exception thrown in first place, calls terminate Handler can always rethrow exception, even if it performed some processing Rethrown exception detected by next enclosing try block

1. Load header 1.1 Function prototype 1 // Fig. 23.2: fig23_02.cpp 2 // Demonstration of rethrowing an exception. 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 #include <exception> 9 10 using std::exception; 11 12 void throwException() 13 { 14 // Throw an exception and immediately catch it. 15 try { 16 cout << "Function throwException\n"; 17 throw exception(); // generate exception 18 } 19 catch( exception e ) 20 { 21 cout << "Exception handled in function throwException\n"; 1. Load header 1.1 Function prototype

2. Function call 3. Output Program Output 22 throw; // rethrow exception for further processing 23 } 24 25 cout << "This also should not print\n"; 26 } 27 28 int main() 29 { 30 try { 31 throwException(); 32 cout << "This should not print\n"; 33 } 34 catch ( exception e ) 35 { 36 cout << "Exception handled in main\n"; 37 } 38 39 cout << "Program control continues after catch in main" 40 << endl; 41 return 0; 42 } 2. Function call 3. Output Program Output Function throwException Exception handled in function throwException Exception handled in main Program control continues after catch in main

Exception specifications Exception specification (throw list) Lists exceptions that can be thrown by a function Example: int g( double h ) throw ( a, b, c ) { // function body } Function can throw listed exceptions or derived types If other type thrown, function unexpected called throw() (i.e., no throw list) states that function will not throw any exceptions In reality, function can still throw exceptions, but calls unexpected (more later) If no throw list specified, function can throw any exception

Processing Unexpected Exceptions Function unexpected Calls the function specified with set_unexpected Default: terminate Function terminate Calls function specified with set_terminate Default: abort set_terminate and set_unexpected Prototypes in <exception> Take pointers to functions (i.E., Function name) Function must return void and take no arguments Returns pointer to last function called by terminate or unexpected

Usages Arithmetic Exceptions -- Divide by Zero Range Exceptions -- Overflow, Underflow Memory Exceptions -- Dynamic Memory Allocations

Acknowledgements These slides were originally development by Dr. Uday Murthy and Dr. Rajeev Raje. Some contents comes from the Deitel slides that accompany your text.