More notes on C++ exceptions

  1. C++ provides support to handle exceptions via a hierarchy of classes.
  2. The class exception is the base class of the exception classes provided by C++.
  3. The function “what” returns the string containing the exception object thrown by C++ built-in exception classes.
  4. The class exception is contained in header file: exception
  5. The 2 classes that are immediately derived from the class exception are logic_error and runtime_error. Both of these classes are defined in the header file: stdexcept
  6. The class invalid_argument is designed to deal with illegal arguments used in a function call.
  7. The class out_of_range deals with the string subscript out_of_range error.
  8. If a length greater than the maximum allowed for a string object is used, the class length_error deals with the error that occurs when a length greater than the maximum size allowed for the object being manipulated is used.
  9. If the operator new cannot allocate memory space, this operator throws a bad_alloc exception.
  10. The class runtime_error is designed to deal with errors that can be detected only during program execution. For example, to deal with arithmetic overflow and underflow exceptions, the class overflow_error and underflow_error are derived from the class runtime_error.
  11. A catch block typically handles the exception or partially processes the exception and then either rethrows the same exception or rethrows another exception in order for the calling environment to handle the exception.
  12. C++ enables programmers to define their own C++ exceptions.
  13. WRGT #12, programmer must throw their own exceptions.
  14. To rethrow: just use “throw” or “throw some-expression”
  15. A function specifies in heading using “throw” clause.
  16. If the program does not handle the exception, then the function terminate is called to terminate the program
  17. See below on how to write your own “terminate function”.

terminate_handler set_terminate (terminate_handler f) throw();

Set terminate handler function

Sets f as the terminate handler function.
A terminate handler function is a function automatically called when the exception handling process has to be abandoned for some reason. This happens when a handler cannot be found for a thrown exception, or for some other exceptional circumstance that makes impossible to continue the handling process.
The terminate handler by default calls cstdlib's abort function.

Parameters

f

Function that takes no parameters and returns void.
The function shall not return and shall terminate the program.
terminate_handler is a function pointer type taking no parameters and returning void.

Return value

The current terminate handler function.
terminate_handler is a function pointer type taking no parameters and returning void.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 / // set_terminate example
#include <iostream>
#include <exception>
#include <cstdlib>
usingnamespace std;
void myterminate () {
cerr < "terminate handler called\n";
abort(); // forces abnormal termination
}
int main (void) {
set_terminate (myterminate);
throw 0; // unhandled exception: calls terminate handler
return 0;
}

Possible output:

terminate handler called
Aborted

See also

terminate / Function handling termination on exception (function)
terminate_handler / Type of terminate handler function (type)
set_unexpected / Set unexpected handler function (function)

From: