Lecture 13
OOP
Exception handling
• An exception is an indication of a problem that occurs during a
program’s execution.
• The name “exception” implies that the problem occurs
infrequently
• Exception handling enables you to create applications that can
resolve (or handle) exceptions.
• In many cases, handling an exception allows a program to continue
executing as if no problem had been encountered.
• enable you to write robust and fault-tolerant programs that can
deal with problems that may arise and continue executing or
terminate gracefully
Consider the following pseudocode
Perform a task
If the preceding task did not execute correctly
Perform error processing
Perform next task
If the preceding task did not execute correctly
Perform error processing
Exceptions: Keywords
Exceptions: Keywords
Exceptions: Keywords
Exceptions: Keyword Try
Exceptions: Keywords
Exceptions: Keyword Throw
Exceptions: Keyword catch
Example
An exception occurred. Exception
No. 20
Exception selection
• Exception handling enables you to remove error-handling code
from the “main line” of the program’s execution
• It improves program clarity and enhances modifiability
• You can decide to handle any exceptions you choose
• all exceptions of a certain type errors
• all exceptions of a group of related types of errors
Exception handling in C++
• The C++ Standard library provides a base class specifically
designed to declare objects to be thrown as exceptions.
• It is defined in the <exception> header.
• An object of exception class is used to tackle the exceptions.
• These objects contains exception details in a program.
• The messages can be overridden during exception handling.
Standard Library Exception Hierarchy
Exception process (try)
• Every exception class that derives directly or indirectly from
exception contains the virtual function what
• What returns an exception object’s error message
• When the exception occurs, the program informs the user of the
mistake
• C++ provides try blocks to enable exception handling.
• A try block consists of keyword try followed by braces ({}) that
define a block of code in which exceptions might occur
Exception process (throw)
• When an exceptional circumstance arises within that block, an
exception is thrown that transfers the control to the exception
handler.
• An exception is thrown by using the throw keyword from inside the
try block
• Normally, a throw statement specifies one operand
• The operand of a throw can be of any type
• function should throw an exception before the error has an
opportunity to occur
Exception process (catch)
• Exceptions are processed by catch handlers (also called exception
handlers), which catch and handle exceptions
• At least one catch handler must immediately follow each try block
• Each catch handler begins with the keyword catch and specifies in
parentheses
• An exception parameter that represents the type of exception
the catch handler can process
• When an exception occurs in a try block
• The catch handler that executes is the one whose type matches
the type of the exception that occurred
When to Use Exception Handling
• Exception handling is designed to process synchronous errors
• This occur when a statement executes
• Some possible exceptions
• out-of-range array subscripts
• Arithmetic overflow
• division by zero
• invalid function parameters
• unsuccessful memory allocation
Exception in Exception handling
• Exception handling is not designed to process errors associated
with asynchronous events
• For example
• disk I/O completions,
• network message arrivals,
• mouse clicks and keystrokes
Rethrowing an Exception
• It’s possible that an exception handler, upon receiving an
exception, might decide either that
• it cannot process that exception
• that it can process the exception only partially
• such cases, the exception handler can defer the exception
handling (or perhaps a portion of it) to another exception handler
• In either case, you achieve this by rethrowing the exception via
the statement throw;
Example Exception Handling
using namespace std; double quotient(int numerator, int denominator)
class myexception : public {
exception if (denominator == 0)
{ {
public: myexception myex;
const char* what() const throw() throw myex;
{ }
return "attempted to divide else
by zero"; return
} static_cast<double>(numerator)/denominator;
}; }
…
int main()
{
int number1, number2;
double result;
cout << "Enter two integers";
cin >> number1 >> number2;
try
{ result = quotient(number1, number2);
cout << "The quotient is: " << result << endl;
}
catch (myexception e)
{ cout << "Exception occurred: "<<e.what()<< endl;
}}
Exception Specifications
• An optional exception specification (also called a throw list)
enumerates a list of exceptions that a function can throw
• For example, consider the function declaration
int someFunction( double value )
throw ( ExceptionA, ExceptionB, ExceptionC )
{
// function body
}
• In this exception specification, which begins with keyword throw
immediately following the closing parenthesis of the function’s
parameter list
…
• This example indicates that function someFunction can throw
exceptions of types ExceptionA, ExceptionB and ExceptionC.
• A function can throw only exceptions of the types indicated by the
specification or exceptions of any type derived from these types
• If the function throws an exception that does not belong to a
specified type
• The exception-handling mechanism calls function unexpected,
which terminates the program.
• A function that does not provide an exception specification can
throw any exception.