Exception handling in C++

In C++ you can write that a compound statement should be tried and that if it fails in specified ways these failures should be caught by handlers. In addition you can throw an exception within such a block to be caught by a handler.

Since a try block (compound statement and attached handlers) is a statement, syntactically, they can be nested.

When an exception is thrown it passes on an object or value of a certain type. This type is matched against those of the handlers for its block and it is handled by the one which matches. If it is not matched, it is passed outwards through nested try blocks. If no match is found at any level, terminate() is called.

A subclass can be used ahead of its parent in a handler list, to catch one of a family of errors.

try{
   ...
   throw "whoops";
   ...
}
catch(const char* s){printf("%s\n",s);}


Next note in series

Back to index