eel3801-12.ppt

Download Report

Transcript eel3801-12.ppt

EEL 3801
Part IX
Fundamentals of C and C++
Programming
Exception Handling
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.
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.
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
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
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.
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.
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( )
{
. . .
}
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.
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.
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.
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.
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
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
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.
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.