bad_exception


class
<exception>
class bad_exception;

Exception thrown by handler unexpected

bad_exception


This is a special type of exception specifically designed to be listed in the exception-specification of a function (i.e., in its throw specifier).

If a function with bad_exception listed in its exception-specification throws an exception not listed in it and unexpected rethrows it (or throws any other exception also not in the exception-specification), a bad_exception is automatically thrown.

Its member what returns a null-terminated character sequence identifying the exception.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// bad_exception example
#include <iostream>
#include <exception>
using namespace std;

void myunexpected () {
  cerr << "unexpected handler called\n";
  throw;
}

void myfunction () throw (int,bad_exception) {
  throw 'x'; // throws char (not in exception-specification)
}

int main (void) {
  set_unexpected (myunexpected);
  try {
    myfunction();
  }
  catch (int) { cerr << "caught int\n"; }
  catch (bad_exception be) { cerr << "caught bad_exception\n"; }
  catch (...) { cerr << "caught other exception (non-compliant compiler?)\n"; }
  return 0;
}


Output:

unexpected handler called
caught bad_exception


See also