![Page 1: CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling](https://reader035.vdocuments.net/reader035/viewer/2022081603/5697c0261a28abf838cd5c85/html5/thumbnails/1.jpg)
CS212: Object Oriented Analysis and DesignLecture 19: Exception Handling
![Page 2: CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling](https://reader035.vdocuments.net/reader035/viewer/2022081603/5697c0261a28abf838cd5c85/html5/thumbnails/2.jpg)
Recap of Lecture 18
• Virtual destructors
• Dispatching, single and multiple
• Downcasting
• Exception handling
![Page 3: CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling](https://reader035.vdocuments.net/reader035/viewer/2022081603/5697c0261a28abf838cd5c85/html5/thumbnails/3.jpg)
Outline of Lecture 19
• Exception handling
• Try, throw catch
• Stack unwinding
• Exception classes
![Page 4: CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling](https://reader035.vdocuments.net/reader035/viewer/2022081603/5697c0261a28abf838cd5c85/html5/thumbnails/4.jpg)
Introduction
• Exception handling allows you to manage run-time errors
• In an orderly fashion
• Program can automatically invoke an error-handling routine
• It automates much of the error-handling code
![Page 5: CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling](https://reader035.vdocuments.net/reader035/viewer/2022081603/5697c0261a28abf838cd5c85/html5/thumbnails/5.jpg)
Error Conditions
• Division by 0, or values that are too large or small for a type
• No memory available for dynamic allocation
• Errors on file access, for example, file not found
• Attempt to access an invalid address in main memory
• Invalid user input
![Page 6: CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling](https://reader035.vdocuments.net/reader035/viewer/2022081603/5697c0261a28abf838cd5c85/html5/thumbnails/6.jpg)
Traditional Error Handling
• Errors in function calls are indicated by special return values
• Global error variables or flags are set or unset when errors occur
if( func()> 0 )// Return value positive => o.k.
else// Treat errors
• Error variables and flags must also be checked after every corresponding action.
• Need to continually check for errors while a program is executing
![Page 7: CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling](https://reader035.vdocuments.net/reader035/viewer/2022081603/5697c0261a28abf838cd5c85/html5/thumbnails/7.jpg)
Exception Handling Concept
• A new approach to error handling
• Keeping the normal functionality of the program separate from error handling
• Errors occurring in one particular part of the program
• Errors are reported to another part of the program, known as the calling environment
• The calling environment performs central error handling.
![Page 8: CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling](https://reader035.vdocuments.net/reader035/viewer/2022081603/5697c0261a28abf838cd5c85/html5/thumbnails/8.jpg)
Exception handling in C++
• C++ exception handling is built upon three keywords: try, catch, and throw.
• Program statements to be monitored are contained in a try block
• If an exception occurs within the try block, it is thrown
• The exception is caught, using catch, and processed
![Page 9: CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling](https://reader035.vdocuments.net/reader035/viewer/2022081603/5697c0261a28abf838cd5c85/html5/thumbnails/9.jpg)
Structure of try-catch block
try {// try block
}catch (type1 arg) {
// catch block}catch (type2 arg) {
// catch block}catch (type3 arg) {
// catch block}...catch (typeN arg) {
// catch block}
Code to be monitored
Possible to have more than one catch for a single try
Demonstration1
![Page 10: CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling](https://reader035.vdocuments.net/reader035/viewer/2022081603/5697c0261a28abf838cd5c85/html5/thumbnails/10.jpg)
Throwing exception
• throw generates the exception specified by exception
• throw must be executed from within a try block itself
• From any function called from within the try block
• Demonstration2
• A try block can be localized to a function
• Demonstration3
![Page 11: CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling](https://reader035.vdocuments.net/reader035/viewer/2022081603/5697c0261a28abf838cd5c85/html5/thumbnails/11.jpg)
Stack Unwinding
• The exception object is then thrown and the program control leaves the try block
• Any changes to the stack that took place after entering the try block are unwound
• The process of removing function entries from function call stack at run time is called Stack Unwinding
• Allows back out of the normal program flow in an orderly manner
• Demonstration
![Page 12: CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling](https://reader035.vdocuments.net/reader035/viewer/2022081603/5697c0261a28abf838cd5c85/html5/thumbnails/12.jpg)
Searching for Handlers
• After the try block the PC searchers for the appropriate catch block
• Always performed sequentially beginning with the first catch block
• Exception declaration of the handler determines whether the handler should be executed
• Identical to the exception type thrown
• A base class of the exception type
• A base class pointer and the exception is a pointer to a derived class
![Page 13: CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling](https://reader035.vdocuments.net/reader035/viewer/2022081603/5697c0261a28abf838cd5c85/html5/thumbnails/13.jpg)
Multiple catch Statements
• It is possible to have multiple condition to throw exception
• For individual throw statement, one catch statement is used
• Similar to switch statement
• Demonstration
![Page 14: CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling](https://reader035.vdocuments.net/reader035/viewer/2022081603/5697c0261a28abf838cd5c85/html5/thumbnails/14.jpg)
Catch all/ Generic handler
• Similar to the tradition switch case, captures any type of exceptions (generic handler)
• A special syntax in the catch statement with an exception declaration consisting of just three dots.
• General exception handler to be defined last
catch( ... ){ // General handler for// all other exceptions}
![Page 15: CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling](https://reader035.vdocuments.net/reader035/viewer/2022081603/5697c0261a28abf838cd5c85/html5/thumbnails/15.jpg)
Exception Classes
• Exception classes are defined to categorize exceptions
• throw statement is used to throw an object belonging to a specific exception class.
• An exception class need not contain data members or methods
• type, which is used by the calling environment to identify the error, is important
• Contains members that provide more specific information on the cause of the error
![Page 16: CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling](https://reader035.vdocuments.net/reader035/viewer/2022081603/5697c0261a28abf838cd5c85/html5/thumbnails/16.jpg)
Continuing the Program
• After executing a handler, the program continues with the first statement following the catch blocks
• Unless the handler throws another exception
• Terminates the program
• After completing exception handling, the exception object that was thrown is destroyed
![Page 17: CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling](https://reader035.vdocuments.net/reader035/viewer/2022081603/5697c0261a28abf838cd5c85/html5/thumbnails/17.jpg)
Catching Class Types
• An exception can be of class types that you create
• Most exceptions will be class types rather than built-in types
• To create an object that describes the error that occurred
• Information can be used by the exception handler to help it process the error
• Demonstration5
![Page 18: CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling](https://reader035.vdocuments.net/reader035/viewer/2022081603/5697c0261a28abf838cd5c85/html5/thumbnails/18.jpg)
Nesting exception handling
• A try block can contain additional try blocks
• This allows using the handlers in a nested try block for special purpose error handling
• Leaving the handlers in the surrounding try block to deal with remaining errors
• You can also nest a try block within a catch block.
![Page 19: CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling](https://reader035.vdocuments.net/reader035/viewer/2022081603/5697c0261a28abf838cd5c85/html5/thumbnails/19.jpg)
try{
// Type1 exceptions are thrown here.try{
// Type1 and Type2 exceptions are thrown here.}catch( Type2 e2){
// Type2 exceptions are pre-handled herethrow; // and thrown again.
}// Other Type1 exceptions can be thrown.
}catch( Type1 e1){
// Type1 exceptions are handled here.}catch(...){
// All remaining exceptions are handled here,// particularly Type2 exceptions.
}
![Page 20: CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling](https://reader035.vdocuments.net/reader035/viewer/2022081603/5697c0261a28abf838cd5c85/html5/thumbnails/20.jpg)
Control transfer in nested case
Try blocks are nested and a throw occurs in a function called by an inner try block
try { func1(); try {
func2(); } catch (spec_err) {
/* ... */ } func3();
} catch (type_err) { /* ... */ } // if no throw is issued, control resumes here.
![Page 21: CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling](https://reader035.vdocuments.net/reader035/viewer/2022081603/5697c0261a28abf838cd5c85/html5/thumbnails/21.jpg)
Standard exceptions
• C++ Standard library provides a base class
• Designed to declare objects to be thrown as exceptions
• Defined in the <exception> header
• This class has a virtual member function called “what”
• Override in derived classes to contain some sort of description of the exception
• Demonstration
![Page 22: CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling](https://reader035.vdocuments.net/reader035/viewer/2022081603/5697c0261a28abf838cd5c85/html5/thumbnails/22.jpg)
Types of exceptions
Exception Description
bad_alloc thrown by new on allocation failure
bad_cast thrown by dynamic_cast when it fails in a dynamic cast
bad_exception thrown by certain dynamic exception specifiers
bad_typeid thrown by typeid
bad_function_call thrown by empty function objects
All exceptions thrown by components of the C++ Standard library throw exceptions derived from this exception class.
![Page 23: CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling](https://reader035.vdocuments.net/reader035/viewer/2022081603/5697c0261a28abf838cd5c85/html5/thumbnails/23.jpg)
Thank youNext Lecture: Exception Handling