Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
Miscellaneous
complex
exception
functional
iterator
limits
locale
memory
new
numeric
stdexcept
typeinfo
utility
valarray
exception
bad_exception
exception
set_terminate
set_unexpected
terminate
terminate_handler
uncaught_exception
unexpected
unexpected_handler


set_unexpected

function
<exception>
unexpected_handler set_unexpected (unexpected_handler f) throw();

Set unexpected handler function

Sets f as the unexpected handler function.

An unexpected handler function is a function automatically called when a function throws an exception that is not in its exception-specification (i.e., in its throw specifier). It may also be called directly by the program.

The unexpected handler function can handle the exception and shall end either by teminating (calling terminate or cstdlib's exit or abort) or by throwing an exception (even rethrowing the same exception again). If the exception thrown (or rethrown) is not in the function's exception-specification but bad_exception is, bad_exception is thrown. Otherwise, if the new exception is not in the exception-specification either, terminate is automatically called.

The unexpected handler by default calls terminate.

Parameters

f
Function that takes no parameters and returns void.
The function shall not return. It shall either throw an exception or terminate.
unexpected_handler is a function pointer type taking no parameters and returning void.

Return value

The current unexpected handler function.
unexpected_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
17
18
19
20
21
22
23
// set_unexpected example
#include <iostream>
#include <exception>
using namespace std;
void myunexpected () {
  cerr << "unexpected called\n";
  throw 0;     // throws int (in exception-specification)
}
void myfunction () throw (int) {
  throw 'x';   // throws char (not in exception-specification)
}
int main (void) {
  set_unexpected (myunexpected);
  try {
    myfunction();
  }
  catch (int) { cerr << "caught int\n"; }
  catch (...) { cerr << "caught other exception (non-compliant compiler?)\n"; }
  return 0;
}


Output:

unexpected called
caught int

See also