Exception Handling
Exception Handling
Handling
Daneshwari M Torgal
Hema Bhooshitha L
Exception Handling
Options
There are several additional features and attributes to C++ exception handling that make
it easier and more convenient to use.
2. Restricting Exceptions
You can restrict the type of exceptions that a function can throw
You can also prevent a function from throwing any exceptions whatsoever.
To accomplish these restrictions, you must add a throw clause to a function
definition. The general form of this is shown here:
ret-type func-name(arg-list) throw(type-list) {
// ...
}
Here, only those data types contained in the comma-separated type-list
may be thrown by the function.
Throwing any other type of expression will cause abnormal program
termination.
If you don't want a function to be able to throw any exceptions, then use an
empty list.
Attempting to throw an exception that is not supported by a function will cause the
standard library function unexpected( ) to be called.
By default, this causes abort( ) to be called, which causes abnormal program
termination
In this program, the function
Xhandler( ) may only throw
integer, character, and double
exceptions. If it attempts to
throw any other type of
exception, an abnormal
program termination will
occur.
return 0;
}
3. Rethrowing an Exception
If you wish to rethrow an expression from within an exception handler, you may
do so by calling throw, by itself, with no exception.
This causes the current exception to be passed on to an outer try/catch sequence.
An exception can only be rethrown from within a catch block (or from any
function called from within that block). When you rethrow an exception, it will
not be recaught by the same catch statement. It will propagate outward to the
next catch statement. The following program illustrates rethrowing an
exception,
Output
Start
Caught char * inside Xhandler
Caught char * inside main End
Thank
You