class
<exception>
std::bad_exception
Exception thrown by unexpected handler
This is a special type of exception specifically designed to be listed in the dynamic-exception-specifier of a function (i.e., in its throw
specifier).
If a function with bad_exception listed in its dynamic-exception-specifier throws an exception not listed in it and unexpected rethrows it (or throws any other exception also not in the dynamic-exception-specifier), a bad_exception is automatically thrown.
Its member what returns a null-terminated character sequence identifying the exception.
Under certain implementations of current_exception (since C++11), this exception is thrown to signal a failed attempt to copy an exception object.
Compatibility
The use of dynamic-exception-specifiers is deprecated (since C++11).
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
// bad_exception example
#include <iostream> // std::cerr
#include <exception> // std::bad_exception, std::set_unexpected
void myunexpected () {
std::cerr << "unexpected handler called\n";
throw;
}
void myfunction () throw (int,std::bad_exception) {
throw 'x'; // throws char (not in exception-specification)
}
int main (void) {
std::set_unexpected (myunexpected);
try {
myfunction();
}
catch (int) { std::cerr << "caught int\n"; }
catch (std::bad_exception be) { std::cerr << "caught bad_exception\n"; }
catch (...) { std::cerr << "caught some other exception\n"; }
return 0;
}
| |
Output:
unexpected handler called
caught bad_exception
|
Exception safety
No-throw guarantee: no members throw exceptions.