0% found this document useful (0 votes)
18 views13 pages

Exception Handling

exception handling

Uploaded by

hemabhooshithal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views13 pages

Exception Handling

exception handling

Uploaded by

hemabhooshithal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Exception

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.

1. Catching All Exceptions


In some circumstances you will want an exception handler to catch all
exceptions instead of just a certain type.
Simply use this form of catch.
catch(...) {
// process all exceptions
}
// This example catches all exceptions.
#include using namespace std;
void Xhandler(int test) {
try{ if(test==0) throw test; // throw int
if(test==1) throw 'a'; // throw char Output
if(test==2) throw 123.23; // throw double Start
} Caught One!
catch(...) { // catch all exceptions Caught One!
cout << "Caught One!\n"; Caught One!
}} End
int main() {
cout << "Start\n";
Xhandler(0);
Xhandler(1);
Xhandler(2);
cout << "End";
return 0; }
As you can see, all three throws were caught using the one catch statement.
For example, this slightly different version of the preceding program explicity
catches integer exceptions but relies upon catch(...) to catch all others.

// This example uses catch(...) as a default.


catch(...) {
#include using namespace std;
// catch all other exceptions
void Xhandler(int test) {
cout << "Caught One!\n";
try{
}}
if(test==0) throw test; // throw int
int main() {
if(test==1) throw 'a'; // throw char
cout << "Start\n";
if(test==2) throw 123.23; // throw double
Xhandler(0);
}
Xhandler(1);
catch(int i) {
Xhandler(2);
// catch an int exception cout
cout << "End";
<< "Caught an integer\n";
return 0; }
}
Output As this example suggests, using catch(...) as a default is a
Start good way to catch all exceptions that you don't want to
Caught an integer handle explicitly. Also, by catching all exceptions, you
Caught One! prevent an unhandled exception from causing an
Caught One! abnormal program termination.
End

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

You might also like