Exception Handling in C++
Exception Handling in C++
catch(int i) { Output:
cout << "Caught Exception #: " << i << '\n';
}
Start
catch(const char *str) { Caught Exception #: 1
cout << "Caught a string: "; Caught Exception #: 2
cout << str << '\n'; Caught a string: Value is zero
} Caught Exception #: 3
} End
Handling Derived-Class Exceptions
• order of catch statements is most
important while inheriting.
• because a catch clause for a base class
will also match any class derived from that
base.
• to catch exceptions of both a base class
and a derived class, put the derived class
first in the catch sequence.
// Catching derived classes. • In this example the catch clause of the
#include <iostream> base class will always be executed.
using namespace std;
class B { • reverse the order of the catch clauses
};
class D: public B {
};
int main()
{
D derived;
try {
throw derived;
}
catch(B b) {
cout << "Caught a base class.\n";
}
catch(D d) {
cout << "This won't execute.\n";
}
return 0;
}
Exception Handling Options
Additional features and nuances that make it
easier and more convenient to use are,
• Catching All Exceptions
• Restricting Exceptions
• Rethrowing an Exception
Catching All Exceptions
• This catch block will catch all kind of
exceptions
• Syntax:
catch(...) {
// process all exceptions
}
// This example catches all exceptions. int main()
#include <iostream> {
using namespace std; cout << "Start\n";
Xhandler(0);
void Xhandler(int test)
Xhandler(1);
{
Xhandler(2);
try{ cout << "End";
if(test==0) throw test; // throw int return 0;
if(test==1) throw 'a'; // throw char }
if(test==2) throw 123.23; // throw double
} Output:
Start
catch(...) { // catch all exceptions Caught One!
cout << "Caught One!\n"; Caught One!
} Caught One!
End
}
This block can be made the last catch block to catch all the
left out exceptions
// This example uses catch(...) as a int main()
default. {
#include <iostream> cout << "Start\n";
using namespace std; Xhandler(0);
void Xhandler(int test) Xhandler(1);
{ Xhandler(2);
try{ cout << "End";
if(test==0) throw test; // throw int return 0;
if(test==1) throw 'a'; // throw char }
if(test==2) throw 123.23; // throw double
} OUTPUT:
catch(int i) { // catch an int exception Start
cout << "Caught an integer\n";
Caught an integer
}
catch(...) { // catch all other exceptions
Caught One!
cout << "Caught One!\n"; Caught One!
} End
}
Restricting Exceptions
• Exceptions thrown outside the functions
can be restricted.
• To accomplish these restrictions, you must
add a throw clause to a function definition.
• Syntax:
ret-type func-name(arg-list) throw(type-list)
{
//..
}
try{
Xhandler(0); // also, try passing 1 and 2 to
Xhandler()
}
// This function can throw NO exceptions!
void Xhandler(int test) throw()
{
/* The following statements no longer work. Instead, they
will cause an abnormal program termination. */
if(test==0) throw test;
if(test==1) throw 'a';
if(test==2) throw 123.23;
}
Rethrowing an Exception
• An exception can be thrown back, by
calling throw, by itself, with no exception.
• the current exception to be passed on to
an outer try/catch sequence.
• This allows multiple handlers access to the
exception.
// Example of "rethrowing" an int main()
exception. {
#include <iostream> cout << "Start\n";
using namespace std; try{
void Xhandler() Xhandler();
{ }
catch(const char *) {
try { cout << "Caught char * inside
throw "hello"; // throw a char * main\n";
} }
cout << "End";
catch(const char *) { // catch a return 0;
char * }
cout << "Caught char * inside
Xhandler\n"; Output:
throw ; // rethrow char * out of Start
function
} Caught char * inside Xhandler
} Caught char * inside main
End
Understanding terminate() and unexpected()
• Eg:
Unwinding the stack because of an exception, a
destructor for an object may throw an exception
bool uncaught_exception( );