CPSC 252 Exception Handling Page 1 Exceptions and exception handling Client programmers can make errors using a class attempting to dequeue an item from.

Slides:



Advertisements
Similar presentations
Chapter 17 Failures and exceptions. This chapter discusses n Failure. n The meaning of system failure. n Causes of failure. n Handling failure. n Exception.
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Lecture 9. 2 Exception  An exception is a unusual, often unpredictable event, detectable by software or hardware, that requires special processing occurring.
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.
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.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 16 Exception Handling.
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.
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.
1 CSC241: Object Oriented Programming Lecture No 28.
Exceptions Briana B. Morrison CSE 1302C Spring 2010.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 16 Exception Handling.
Exception Handling Introduction Exception handling is a mechanism to handle exceptions. Exceptions are error like situations. It is difficult to decide.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
Exceptions Used to signal errors or unexpected situations to calling code Should not be used for problems that can be dealt with reasonably within local.
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
Rossella Lau Lecture 9, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 9: Application with 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.
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.
Introduction to C++ Templates and Exceptions l C++ Function Templates l C++ Class Templates l Exception and Exception Handler.
June 14, 2001Exception Handling in Java1 Richard S. Huntrods June 14, 2001 University of Calgary.
Object Oriented Programming
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?? }
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.
Exceptions Handling Exceptionally Sticky Problems.
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.
HANDLING EXCEPTIONS Chapter 9. Outline  Learn about the limitations of traditional error-handling methods  Throw exceptions  Use try blocks  Catch.
CS212: Object Oriented Analysis and Design Lecture 20: Exception Handling-II.
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.
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.
Exceptions Lecture 11 COMP 401, Fall /25/2014.
C++ Namespaces, Exceptions CSci 588: Data Structures, Algorithms and Software Design All material not from online sources copyright © Travis Desell, 2011.
Exception Handling How to handle the runtime errors.
CHAPTER 18 C – C++ Section 1: Exceptions. Error Handling with Exceptions Forces you to defend yourself Separates error handling code from the source.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Introduction to Exceptions in Java CS201, SW Development Methods.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
2.4 Exceptions n Detects try { //code that may raise an exception and/or set some condition if (condition) throw exceptionName; //Freq. A string } n Handles.
C ++ MULTIPLE CHOICE QUESTION
Exception handling.
Chapter 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.
Exceptions.
CMSC202 Computer Science II for Majors Lecture 16 – Exceptions
Handling Exceptionally Sticky Problems
Why exception handling in C++?
Exceptions C++ Interlude 3
Chapter 14: Exception Handling
Exceptions with Functions
Exception Handling Chapter 9 Edited by JJ.
Exceptions 1 CMSC 202.
Handling Exceptionally Sticky Problems
CMSC 202 Exceptions.
Exception Handling.
ENERGY 211 / CME 211 Lecture 24 November 14, 2008.
CMSC 202 Lesson 20 Exceptions 1.
Presentation transcript:

CPSC 252 Exception Handling Page 1 Exceptions and exception handling Client programmers can make errors using a class attempting to dequeue an item from an empty queue even though we have placed a precondition on the method Item_type Queue::dequeue( void ) // Pre: Queue is not empty the client might not check before calling the dequeue() What should the dequeue() member function do? More generally, what should any function do when an error arises?

CPSC 252 Exception Handling Page 2 Possible ways to handle errors Do nothing and run the risk of severe problems such as a segmentation fault resulting in abnormal program termination Check for the error (the failing precondition) and return a default value if necessary and run the risk that the client programmer may never clue into the fact that something went wrong, leading to unexpected results (or worse) for the client code Check for the error condition and alert the client that something went wrong Option 3 is by far the better approach! How do we inform the client that something went wrong?

CPSC 252 Exception Handling Page 3 Notifying the client Terminate the program and optionally print an error message if ( !count ) { cerr << “Queue is empty” << endl; exit (-1); } else not great because the client cannot recover Have the function return a bool indicating successful/failure bool enqueue( const Item_type& item ) reasonable when we know failure is expected at some point but not when something unexpected happens only seldomly

CPSC 252 Exception Handling Page 4 Or we can throw an exception… C++ has a special mechanism for signaling failure any function can throw an exception when it detects an error if ( count >= CAPACITY ) throw logic_error( “Queue is full” ); else this terminates the call to the function the client code then has the option of catching the exception catch ( exception& e ) { [error handling] } the client might not catch the exception if it is not caught a default handler will terminate the program

CPSC 252 Exception Handling Page 5 What is an exception? Is it an int? Is it a char? In C++, any kind of object can be thrown as an exception usually the variable that is thrown is a class these are designed specifically to act as exceptions C++ defines a set of these classes in the standard library the declarations are in the header file exception logic_errorruntime_error

CPSC 252 Exception Handling Page 6 The what() method in the exception class The exception class uses this to describe the error what() has the following signature const char* what() const; what() returns a pointer to an error message the constructor has a string parameter for the message We can create our own exception classes they are based on logic_error and runtime_error a logic_error indicates the client should know better a runtime_error indicates external cause a bad_alloc from new means no memory available

CPSC 252 Exception Handling Page 7 We define an exception that can be thrown when the dequeue function is called on an empty queue We will call this exception AccessViolation to indicate that we are trying to access an element that does not exist #include #include // probably includes using namespace std; class AccessViolation : public logic_error { AccessViolation( void ) : logic_error( “Access violation” ) { } }; The what() method is declared in the exception class

CPSC 252 Exception Handling Page 8 Now let’s think about how our dequeue() function will be written: Item_type Queue::dequeue( void ) { if( isEmpty() ) // check if queue is empty throw AccessViolation(); // rest of function omitted, but it does // whatever we do if queue is not empty } Here we call the constructor of the AccessViolation class to create an anonymous instance of the class and throw it If the exception is thrown, the dequeue() function is terminated without executing any subsequent code in the function

CPSC 252 Exception Handling Page 9 How the client uses the exception try { while( true ) // Horrible! Should be myQueue.dequeue(); // checking precondition! } catch( exception& e ) { cout << “Exception: ” << e.what() << endl; } // now continue with rest of program There are two subtleties here that we will not fully explain the object e is a reference to an exception the what() function retrieves “Access violation”

CPSC 252 Exception Handling Page 10 What this means to a client Any code that calls a function that might throw an exception calls a function that calls a function that might throw an exception etc. should be wrapped in try-catch blocks we try to execute the code in the try -block if problems arise we let the catch -block handle it This passes the responsibility for dealing with the error condition back to the client rather where it belongs Do not to overuse this feature!

CPSC 252 Exception Handling Page 11 Some words of caution about exceptions Note: If a function can reasonably handle the error condition itself then it should do so. An exception should be thrown only in the case where the function cannot be expected to handle the situation. Note: The fact that a function throws an exception is not an excuse for the client to write sloppy code. Remember that an exception should be thrown only when something exceptional occurs. Note: do not throw exceptions to “bail out” from situations that you as a programmer have difficulty resolving – for example, bailing out from a recursive call because you can’t think of any other way to do it gracefully!

CPSC 252 Exception Handling Page 12 Example of a use of exceptions (1) Suppose the employee has two data members name – to represent the employee’s last name employeeNum - to represent the employee number class employee { string name; int employeeNum; }; Now suppose we want to write a function to read an employee from a file We can run into two problems when we use the function: we hit the end of file so there is no more data to read the data in the file is not a valid employee number

CPSC 252 Exception Handling Page 13 Example of a use of exceptions (2) The last situation will occur if the file contains the following data, for example: Jones 3542 Wong 4214 Smith Ng 6785 one of these situations is expected (the end of file) and the other (invalid format) is not we handle them accordingly a return value for end-of-file an exception for invalid data

CPSC 252 Exception Handling Page 14 Example of a use of exceptions (3) bool readEmployee( ifstream& inStream, employee& next ) { inStream >> next.name; if( inStream.eof() ) return false; inStream >> next.employeeNum; if( inStream.fail() ) throw BadDataError(); return true; } the end-of-file is expected at some point we return a bool and let the client check the return value finding badly formatted data in the file is not expected we handle this situation by throwing an exception

CPSC 252 Exception Handling Page 15 Some of the fine details We can have more than one catch block this allow us to catch different types of exceptions We catch any type of exception try { // do something that might throw an exception } catch( AccessViolation& excep ) { // do this if AccessViolation } catch( exception& excep ) { // do this if some other exception class } catch(... ) { // do this for any other type that is thrown }

CPSC 252 Exception Handling Page 16 Which catch block handles the exception? If an exception is thrown from the try block, it is handled by the first catch block that can accept it based on its type in the previous example, if the exception that gets thrown is a AccessViolation the exception is handled by the first catch block (and only that block) if the exception that gets thrown cannot be handled by the first block it is tested against subsequent catch blocks if it cannot be handled by any of them, it is handled by the catch-all block. it is important to order catch blocks so that earlier blocks handle the more specific exceptions the catch-all block, if there is one, must always go last