![[Dept of Engineering]](http://www.eng.cam.ac.uk/images/house_style/engban-s.gif)
There are many types of exception - they form a class hierarchy of their own. Here just a few will be introduced.
try {
// code
}
// catch a standard exception
catch(std:exception& e){
// order of the catch clauses matters; they're tried
// in the given order
}
// catch all the other types
catch(...) {
// cleanup
throw; // throw the exception up the hierarchy
}
Here's an example that sooner or later will produce an exception.
try{
for(;;)
new char[10000];
}
catch(bad_alloc) {
cerr << "No memory left";
}
An exception example is online.
You can override the routine that handles problems encountered by the new operator
set_new_handler(&my_new_handler);
For safety, you can declare which exceptions a function should be able to throw
int f() throw (std::bad_alloc) // f may only throw bad_alloc
A useful exception is
out_of_range (header <stdexcept>), which is thrown by at() and by
bitset<>::operator[]().
Exception handling code can slow a program down even when no exceptions happen. The code size will also increase.