HANDLING EXCEPTIONS Chapter 9. Outline  Learn about the limitations of traditional error-handling methods  Throw exceptions  Use try blocks  Catch.

Slides:



Advertisements
Similar presentations
Topics Introduction Types of Errors Exceptions Exception Handling
Advertisements

 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Exception Handling Outline 13.1 Introduction 13.2 Exception-Handling Overview 13.3 Other.
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.
Review Linked list: Doubly linked list, insertback, insertbefore Remove Search.
1 Handling Exceptions COSC 1567 C++ Programming Lecture 11.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 16 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.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
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.
Exception Handling 1 CISC6795, Spring Introduction 2 Exception – an indication of a problem that occurs during a program’s execution, for examples:
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.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
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.
Rossella Lau Lecture 9, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 9: Application with Exception Handling 
Exception Handling An Exception is an indication of a problem that occurs during a program’s execution. Exception handling enables the programmer to create.
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.
Dale Roberts Exception Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
CPSC 252 Exception Handling Page 1 Exceptions and exception handling Client programmers can make errors using a class attempting to dequeue an item from.
1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about.
Object Oriented Programming
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 13 - Exception Handling Outline 13.1Introduction 13.2When Exception Handling Should Be Used.
Chapter 12: Exception Handling
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.
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Exceptions and Assertions Chapter 15 – CSCI 1302.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
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.
CSCI 383 Object-Oriented Programming & Design Lecture 20 Martin van Bommel.
LECTURE LECTURE 14 Exception Handling Textbook p
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
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.
EE4E. C++ Programming Lecture 6 Advanced Topics. Contents Introduction Introduction Exception handling in C++ Exception handling in C++  An object oriented.
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.
C ++ MULTIPLE CHOICE QUESTION
Exception handling.
Exception Handling in C++
IS 0020 Program Design and Software Tools
16 Exception Handling.
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
Chapter 14: Exception Handling
Exception Handling Chapter 9 Edited by JJ.
Throwing and catching exceptions
Object-Oriented Programming Using C++ Second Edition
16 Exception Handling.
Presentation transcript:

HANDLING EXCEPTIONS Chapter 9

Outline  Learn about the limitations of traditional error-handling methods  Throw exceptions  Use try blocks  Catch exceptions  Use multiple throw statements and multiple catch blocks  Use the default exception handler  Understand exception classes in the Standard Runtime Library  Derive your own exceptions from the exception class  Use exception specifications  Learn about unwinding the stack  Handle memory-allocation exceptions  Learn when to use exception handling 2

UNDERSTANDING THE LIMITATIONS OF TRADITIONAL ERROR HANDLING METHODS perform a first task if there was an error, perform some error processing else perform a second task if there was an error perform some error processing else perform a third task...and so on until the job is complete 3

 The error handling is interspersed with the logic of the program, making the program harder to read and maintain.  this example, each subsequent task is performed only if the previous task was successful, the number of nested decisions can become large, also making the program harder to read, and increasing the chance for mistakes.  The program is less efficient than it could be because decisions are constantly being made to test for situations that rarely occur.  Further complications make this approach unwieldy. The pseudocode contains several decisions “if there was an error” without indicating how the error is detected.  One possibility is that each of the tasks listed in the pseudocode is a function call, -> if each of the functions returns a code, then each function must be written so it returns nothing else UNDERSTANDING THE LIMITATIONS OF TRADITIONAL ERROR HANDLING METHODS 4

5

 general rule of modular programming that provides the most flexibility is that a function should be able to determine an error situation, but not necessarily take action on it.  A better alternative to the function is to let the function discover the error, and then to notify the calling function of the error so the calling function can decide what to do about it.  -> flexibility by letting the calling function decide whether to carry on or terminate when the invalid data entry occurs.  This approach promotes function reusability—the getUserNumber() function can be used no matter what actions are needed after invalid data is entered.  Fortunately, object-oriented programming languages provide you with techniques that circumvent the problems of traditional error handling. -> exception handling; the actions you take with exceptions involve trying, throwing, and catching them.  try a function; if it throws an exception, you catch and handle it 6 UNDERSTANDING THE LIMITATIONS OF TRADITIONAL ERROR HANDLING METHODS

THROWING EXCEPTIONS  A function can contain code to check for errors, and then send a message when it detects an error.  an exception is an object that contains information that is passed from the place where a problem occurs to another place that will handle the problem.  an exception object can be of any data type, including a scalar or class type,  and a variety of exception objects of different types can be sent from the same function, regardless of the function’s return type.  true to object-oriented style, you can create your own exception types that inherit from a built-in base exception class  any called function should check for errors, but should not be required to handle an error if one is found.  For example, suppose you call your hair stylist to make a 1 o’clock appointment on Thursday, and suppose the stylist has no openings then.  Maximum flexibility is achieved when the calling function is responsible for handling the error detected by the called function. 7

 When an object-oriented program detects an error within a function, the function should send an error object to the calling function, or throw an exception.  A throw resembles a return statement in a function: does not continue from where the function was called.  It also resembles an exit()call, except that control does not have to be returned to the operating system.  Instead, when you throw an exception, control continues at a new program location chosen by the programmer where the program can catch, or receive, the exception that was thrown. 8 THROWING EXCEPTIONS

9

10 THROWING EXCEPTIONS

USING TRY BLOCKS  A try block includes the following components:  the keyword try  an opening curly brace  the statements that are tried  a closing curly brace 11

12 USING TRY BLOCKS

CATCHING EXCEPTIONS  A catch block contains statements that execute when an exception is thrown and includes the following components:  the keyword catch  a single parameter in parentheses  an opening curly brace  one or more statements that describe the exception action to be taken  a closing curly brace 13

14

USING MULTIPLE THROW STATEMENTS AND MULTIPLE CATCH BLOCKS #include using namespace std; int getPositiveSingleDigit() { int userEntry; const int LOW = 0; const int HIGH = 9; const string ERROR_MSG = "Negative number"; cout << "Enter a single-digit positive value "; cin >> userEntry; if(userEntry < LOW) throw(ERROR_MSG); if(userEntry >HIGH) throw(userEntry); return userEntry; } int main() { int value; try { value = getPositiveSingleDigit(); cout << "The entered value was good" << endl; } catch(string msg) { cout << "A message was thrown " << endl; cout << msg << endl; } catch(int badValue) { const int REDUCTION = 10; value = badValue % REDUCTION; cout << "A value was thrown " << endl; cout << "The number you entered, " << badValue << ", is too large." << endl; cout << "So it is being reduced to " << value << endl; } cout << "The value at the end of the program is “ << value << endl; return 0; } 15

DETERMINING THE ORDER OF catch BLOCKS  the first catch block that can accept a thrown object is the one that will execute.  When you create a function that throws several types, such as an integer and an Employee, it doesn’t matter which catch block you place first.  However, if you need to throw both a base class object and an object that is a member of its derived class from the same function:  For example, if you create an Employee class and a child PartTimeEmployee class, and either might be thrown from a function, then you must code the catch block for the PartTimeEmployee object first. 16

USING THE DEFAULT EXCEPTION HANDLER  When any object is thrown with a throw statement, then a subsequent catch block has a usable match if one of the following conditions is true:  The type of the thrown object and the type of the catch parameter are identical (for example, int and int).  The type of the thrown object and the type of the catch parameter are the same, int can be caught by const int, int&, or const int&  The catch parameter type is a parent class of the thrown argument. For example, if PartTimeEmployee derives from Employee, a catch block with an Employee parameter can catch both.  If you throw an exception and no catch block exists with an acceptable parameter type, then the program terminates -> code a default exception handler or default catch block that catches any type of object not previously caught. 17

UNDERSTANDING EXCEPTION CLASSES IN THE STANDARD RUNTIME LIBRARY 18

 logic_errors are errors in preconditions of some operation, such as passing an invalid value to a function  runtime_errors include problems that occur during program execution.  overflow_error occurs when the result of an arithmetic operation is larger than the largest number that can be stored on a computer 19 UNDERSTANDING EXCEPTION CLASSES IN THE STANDARD RUNTIME LIBRARY

 A bad_alloc exception occurs when you attempt to allocate additional memory and an insufficient amount exists.  The dynamic_cast operator converts pointers from parent to child types. A bad_cast exception occurs when a dynamic_cast to a reference type fails.  A bad_exception is generated if you rethrow an exception from within an unexpected handler. You will learn about rethrowing exceptions later in this chapter.  The typeid operator returns information about an object’s data type. A bad_typeid exception is generated if you attempt to apply the typeid operator to a null expression. 20 UNDERSTANDING EXCEPTION CLASSES IN THE STANDARD RUNTIME LIBRARY

AN EXAMPLE OF AN AUTOMATICALLY THROWN logic_error 21

USING THE what()FUNCTION 22

EXPLICITLY THROWING A BUILT-IN exception 23

24

DERIVING YOUR OWN EXCEPTIONS FROM THE EXCEPTION CLASS  making your programs easier for object-oriented programmers to understand.  Exception-handling using the built-in classes as a basis provides a more uniform way to handle errors. This is beneficial when many programmers combine multiple modules to create a large project.  If all exceptions derive from exception or one of its subclasses, then when you want to handle several program exceptions in the same way, you include a catch block with an exception parameter.  When an exception is of type exception or one of its subclasses, you can pass a string to the constructor that can be used as an informative message returned from the what() function.  override the parent class’s what()function with your own version and programmers who use your exception class will understand its purpose. 25

26

#include using namespace std; int main() { const int LOW = 3; const int HIGH = 7; int score; int badUserValue; int difference; try { score = getValueInRange(LOW, HIGH); } catch(RangeException e) { cout << e.what() << endl; badUserValue = e.getUser(); if(badUserValue < LOW) { difference = LOW - badUserValue; cout << "The entry was " << difference << " too low" << endl; } else { difference = badUserValue - HIGH; cout << "The entry was " << difference <<" too high" << endl; } score = 0; } cout << "Score is " << score << endl; return 0; } 27 DERIVING YOUR OWN EXCEPTIONS FROM THE EXCEPTION CLASS

28

OVERRIDING THE EXCEPTION CLASS what()FUNCTION  Other programmers will immediately understand the purpose of and know how to use what()with exception objects.  If a client program catches a group of exceptions, such as all exceptions, or all runtime_exceptions, then one catch block can handle all of them, but use what() with the generic parameter to display the message appropriate to the caught subtype. 29

30 OVERRIDING THE EXCEPTION CLASS what()FUNCTION

USING EXCEPTION SPECIFICATIONS  how many different types of objects a function throws if you fail to carefully examine the code  You can explicitly indicate the exceptions that a function can possibly throw by writing an exception specification, which is a declaration of a function’s possible throw types  An exception specification is also called a throw list  int dataEntry() throw(char, double, Employee)  int dataEntry() throw() 31

EXCEPTION SPECIFICATIONS IN ANSI C++  American National Standards Institute (ANSI)  In ANSI C++, if a function throws an error whose type was not listed in its exception specification, then it will produce a run-time error called an unexpected exception, and abort the program. 32

HOW VISUAL C++ DEPARTS FROM THE ANSI STANDARD  throw()means the function does not throw an exception  throw(...)means the function can throw an exception  throw(type)means the function can throw an exception of type 33

 if function A calls function B, function B calls function C, and function C throws an exception, then if B does not catch the exception, function A can. If no HANDLING EXCEPTIONS function catches the exception, then the program terminates. This process is called unwinding the stack 34 UNWINDING THE STACK

35 UNWINDING THE STACK

36

37

38

39

RETHROWING EXCEPTIONS 40

HANDLING MEMORY ALLOCATION EXCEPTIONS 41

WHEN TO USE EXCEPTION HANDLING 42