Download presentation
Presentation is loading. Please wait.
Published byMadeleine Griffith Modified over 6 years ago
1
Part IX Fundamentals of C and C++ Programming Exception Handling
EEL 3801 Part IX Fundamentals of C and C++ Programming Exception Handling
2
Exception Handling Used in situations when:
Errors can potentially occur and the system can recover from them. Errors are synchronous in nature e.g., memory exhaustion division by zero arithmetic overflow out of bounds array subscript Errors will be dealt with in a different part of the program.
3
Exception Handling Not used in situations when:
Errors are asynchronous in nature (e.g., network messages, disk I/O errors, mouse click errors). Interrupt processing are a better choice of techniques for these types of errors.
4
Exception Handling in C++
Tries a block of code that may contain exceptions Throws an exception when one is detected. Catches the exception and handles it. Thus, there are three concepts: the try block the throwing of the exceptions the block that catches or handles the exception
5
The try Block A block which includes the code that may generate an error (an exception). try { .. .. } Can be followed by one or more catch blocks which handle the exception
6
The try Block Control of the program passes from the statements in the try block, to the appropriate catch block. The throw point may be deeply nested within the try block Functions called from the try block, directly or indirectly, could test for the presence of the error.
7
The throw Point Used to indicate that an exception has occurred.
It is called “throwing an exception” Throw normally specifies an operand: can be of any type can be an object (called the exception object) Will be caught by closest exception handler.
8
The catch Blocks Contain the exception handler.
These know what to do with the exception - typically print out that a type of error has occurred. catch( ) { . . . }
9
Catching Exceptions catch blocks are typically located right after the try block that could throw the exception. The exception will be “caught” by the closest exception handler that specifies the proper type.
10
Exception Handling Procedure
When exception identified in the try block control passes from the try block to the closest catch block that meets the type criteria. The code in correct catch block is executed. Control passes beyond the last catch block if there are no exceptions. An exception not caught will cause program to terminate prematurely.
11
Exception Handling Procedure
Exception Handlers searched in order for an appropriate match. If more than 1 matches are appropriate, then the physically closest to the try block with threw the exception. The programmer determines the order of the catch blocks. Once an exception is thrown, control cannot return to the throw point.
12
Exception Handling - Example
#include <iostream.h> class DivideByZeroError() { public: DivideByZeroError() : message(“Divide by Zero”) {}; void printMessage() const {cout << message ; } private: const char *message; This is called the error class.
13
Exception Handling - Example
float quotient(int num1, int num2) { if (num2 == 0) throw DivideByZeroError(); return (float) num1/num2; } Throws an exception to the error class DivideByZeroError
14
Exception Handling - Example
main() { cout << “Enter 2 integers”; int num1, num2. cin >> num1 >> num2; try { float result = quotient(num1, num2); cout << “the quotient is” << result; } . . . // continued on next slide
15
Exception Handling - Example
catch (DivideByZeroError error) { cout << “ERROR:” error.printMessage() cout << endl; return 1; } return 0; ) An object of class DivideByZeroError is instantiated and named error.
16
Example Discussion Note that the detection element, quotient is embedded indirectly into the try block. Note that the error message contained in the exception class is printed out. The program control skips the catch block if an exception is not thrown. Several catch blocks can be placed in sequence after the try block.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.