Exception Handling Introduction Try Throw Catch. Exception Handling Use exception handling to write programs that are –Clearer –More robust –More fault-tolerant.

Slides:



Advertisements
Similar presentations
Exception Handling. Introduction Errors can be dealt with at place error occurs –Easy to see if proper error checking implemented –Harder to read application.
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.
Exceptions, Templates, And The Standard Template Library (STL) Chapter 16.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Exception Handling Handling an unexpected behaviour Unit - 08.
 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.
Copyright © 2012 Pearson Education, Inc. Chapter 16: Exceptions, Templates, and the Standard Template Library (STL)
Dale Roberts Exception Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
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.
Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  due to poor understanding of the problem and solution.
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.
© Copyright Eliyahu Brutman Exceptions. © Copyright Eliyahu Brutman Exceptions and Design Patterns - 2 Introduction to Exception Handling Definition:
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
Exceptions Objectives At the conclusion of this lesson, students should be able to Explain the need for exceptions Correctly write programs that use.
Chapter 11: Handling Exceptions and Events J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Fourth.
Exceptions COMPSCI 105 S Principles of Computer Science.
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 Chapter 14 - Exception Handling Outline 14.1Introduction 14.2When Exception Handling Should Be Used 14.3Other Error Handling Techniques 14.4The Basics.
Object Oriented Programming
CIS 270—Application Development II Chapter 13—Exception Handling.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 13 - Exception Handling Outline 13.1Introduction 13.2When Exception Handling Should Be Used.
Handling ErrorstMyn1 Handling Errors Up to this point we haven't worried much about errors or exceptions. First, let's distinguish between errors and exceptions.
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.
Operator Overloading & Exception Handling TCP1201 OOPDS 1 Lecture 5 1.
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.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
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.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 14 - Exception Handling Outline 14.1Introduction 14.2When Exception Handling Should Be Used 14.3Other.
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.
LECTURE LECTURE 14 Exception Handling Textbook p
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
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.
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.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Object-Oriented Programming in C++ Exception.
Exception handling.
Exceptions Error Handling and Recovery
16 Exception Handling.
EXCEPTION HANDLING IN C++
Andy Wang Object Oriented Programming in C++ COP 3330
Why exception handling in C++?
Part IX Fundamentals of C and C++ Programming Exception Handling
EXCEPTION HANDLING.
Chapter 14: Exception Handling
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 17 Templates and Exceptions Part 2
Exception Handling.
Exceptions 1 CMSC 202.
Object-Oriented Programming (OOP) Lecture No. 44
Department of Computer and Information Science, School of Science, IUPUI Exception Handling Dale Roberts, Lecturer Computer Science, IUPUI
Exception Handling.
CMSC 202 Lesson 20 Exceptions 1.
Presentation transcript:

Exception Handling Introduction Try Throw Catch

Exception Handling Use exception handling to write programs that are –Clearer –More robust –More fault-tolerant Exception examples –Failure of new to obtain enough memory –An out-of-bounds array subscript –Arithmetic overflow –Division by zero –Invalid function parameters

Error-handling Code Typically error-handling code is put where errors are expected to occur –Advantage Programmers can easily check to if error handling has been done in the immediate vicinity of the code –Disadvantage Code may become cluttered with too much error processing

C++’s Exception Handling Using C++’s exception handling removes most of the error handling code from the main program –Improves program readability and modifiability –Can handle “synchronous errors” Such as division by zero –Cannot handle “asynchronous situations” Such as network message arrivals Mouse clicks

C++’s Exception Handling Exception handling is used to –Allow the system to recover from an exception –Or (in the case of an unrecoverable error) clean up the mess & shut down gracefully

The Basics: try, throw, & catch Code that may generate an exception is encased in a try block –Within the try bock an exception is thrown After the try block, a catch block will catch and handle the exception, so that the program can return to its normal state –Program will search for a catch block with the same parameter type as the exception type If an exception is not handled, the program will terminate

Exception Class & Function class DBZException { public: DBZException() :message("ERROR: division by zero"){} void print() const {cout<<message<<endl;} private: const char *message; }; double quotient( int num, int den ){ if ( den == 0 ) throw DBZException(); return static_cast (num)/den; } (See DBZException.cpp.txt)DBZException.cpp.txt

Main int main(){ int num1, num2; double result; cout << "Enter two integers (EOF to end): "; while ( cin >> num1 >> num2 ) { try { result = quotient( num1, num2 ); cout<<"The quotient is: "<<result<<endl; } catch ( DBZException ex ) { ex.print(); } cout<<"\nEnter two integers (EOF to end): "; } return 0; }

Output Even thought the user made a mistake, the program can continue without an abend (ABnormal END) Enter two integers (EOF to end): 3 4 The quotient is: 0.75 Enter two integers (EOF to end): 4 0 ERROR: division by zero Enter two integers (EOF to end): 4 5 The quotient is: 0.8

End-of-File Marker File ends with a end-of-file (EOF) marker –EOF key combinations on different systems: UNIX systems: d IBM PC and compatibles: z Macintosh: d

Throwing an Exception A throw indicates that an exception occurred –The throw operand can be of any type –Can throw objects not intended for error handling When an operand is thrown –A temporary copy is made & initialized When this operand is caught –It initializes exception handler’s parameter When the exception handler completes execution and exits, the temporary object is destroyed

Throwing an Exception II If a handler is not found within a try block –The program will search for a matching handler in the next nested try block, until it is found All exceptions thrown outside a try block will terminate the program All objects declared in a try block will be destroyed before an exception is thrown from that block

New Exception Class class DBZException { public: DBZException():message("ERROR: division by zero"){} void print() const {cout<<message<<endl; } private: const char *message;}; class NIFException { public: NIFException() :message("ERROR: numerator is five"){} void print() const { cout<<message<<endl; } private: const char *message; };

Functions void checkNumeratorIs5(int n){ if(n==5) throw NIFException(); if(n==100) throw int(); } double quotient( int num, int den ){ if ( den == 0 ) throw DBZException(); try{ checkNumeratorIs5(num); cout<<"No exception in QUOTIENT()"<<endl;} catch(int e){ cout<<"Type INT exception thrown"<<endl;} return static_cast ( num ) / den; }

Main int main(){ int num1, num2; double result; cout << "Enter two integers (EOF to end): "; while ( cin >> num1 >> num2 ) { try{ result = quotient( num1, num2 ); cout<<"The quotient is:"<<result<<endl;} catch ( DBZException e ) { e.print(); } catch ( NIFException e ) { e.print(); } cout<<"\nEnter two integers (EOF to end): "; } return 0;}

Output Enter two integers (EOF to end): 1 2 No exception in QUOTIENT() The quotient is: 0.5 Enter two integers (EOF to end): 5 2 ERROR: numerator is five Enter two integers (EOF to end): Type INT exception thrown The quotient is: 25 (See throwing.cpp.txt)throwing.cpp.txt

Catching an Exception The catch block is used to 1.Catch an exception 2.Execute the code within the block The parameter in a catch statement can be named or unnamed –The handler catch(…) will catch any exception If no matching parameter type is found for a thrown object, the program terminates

Catching an Exception II A catch block can throw another kind of exception or re-throw itself –These exceptions will not be caught by any catch blocks listed after the current catch block –They will be caught after the next outer try block –If not caught, they will terminate the program

Exception Class class DBZException { public: DBZException() :message("ERROR: division by zero"){} void print() const {cout<<message<<endl;} private: const char *message; }; (See catching.cpp.txt)catching.cpp.txt

Functions void check(int n){ if(n==100) throw int(); } double quotient( int num, int den ){ if ( den == 0 ) throw DBZException(); try{ check(num); } catch( int ){ //unnamed parameter cout<<"Type INT exception thrown"<<endl; throw; //re-throw exception } return static_cast ( num ) / den; }

void main(){ int num1, num2; double result; cout << "Enter two integers (EOF to end): "; try{ while ( cin >> num1 >> num2 ) { try { result = quotient( num1, num2 ); cout<<"The quotient is:"<<result<<endl;} catch ( DBZException e ) { e.print(); throw float(); } catch (...) { cout<<"UNDEFINED ERROR I"<<endl; } cout<<"\nEnter two integers : ";} } catch(...){ cout<<"UNDEFINED ERROR II"<<endl;} }

Output Enter two integers (EOF to end): Type INT exception thrown UNDEFINED ERROR I Enter two integers (EOF to end): ERROR: division by zero UNDEFINED ERROR II (program terminates)

Class Exercise 1 See exercise1.txtexercise1.txt