Difference between revisions of "cpp/error/bad exception"
From cppreference.com
m (r2.7.3) (Robot: Adding de, es, fr, it, ja, pt, ru, zh) |
m (Shorten template names. Use {{lc}} where appropriate.) |
||
Line 1: | Line 1: | ||
{{cpp/title|bad_exception}} | {{cpp/title|bad_exception}} | ||
{{cpp/error/navbar}} | {{cpp/error/navbar}} | ||
− | {{ | + | {{dcl begin}} |
− | {{ | + | {{dcl header | exception}} |
− | {{ | + | {{dcl |1= |
class bad_exception; | class bad_exception; | ||
}} | }} | ||
− | {{ | + | {{dcl end}} |
{{tt|std::bad_exception}} is the type of the exception thrown by the C++ runtime in the following situations: | {{tt|std::bad_exception}} is the type of the exception thrown by the C++ runtime in the following situations: | ||
− | @1@ If a [[cpp/language/except_spec|dynamic exception specification]] is violated and {{ | + | @1@ If a [[cpp/language/except_spec|dynamic exception specification]] is violated and {{lc|std::unexpected}} throws or rethrows an exception that still violates the exception specification, but the exception specification allows {{tt|std::bad_exception}}, {{tt|std::bad_exception}} is thrown. |
− | @2@ If {{ | + | @2@ If {{lc|std::exception_ptr}} stores a copy of the caught exception and if the copy constructor of the exception object caught by {{c|current_exception}} throws an exception, the captured exception is an instance of {{tt|std::bad_exception}}. |
{{inheritance diagram/std-bad_exception}} | {{inheritance diagram/std-bad_exception}} | ||
===Member functions=== | ===Member functions=== | ||
− | {{ | + | {{dsc begin}} |
− | {{ | + | {{dsc mem ctor | cpp/error/bad_exception/bad_exception | constructs the {{tt|bad_exception}} object}} |
− | {{ | + | {{dsc mem fun | cpp/error/bad_exception/operator{{=}} | copies the object}} |
− | {{ | + | {{dsc mem vfun | cpp/error/bad_exception/what | returns the explanatory string}} |
− | {{ | + | {{dsc end}} |
{{cpp/error/exception/inherit}} | {{cpp/error/exception/inherit}} |
Revision as of 18:41, 31 May 2013
Defined in header <exception>
|
||
class bad_exception; |
||
std::bad_exception
is the type of the exception thrown by the C++ runtime in the following situations:
1) If a dynamic exception specification is violated and std::unexpected throws or rethrows an exception that still violates the exception specification, but the exception specification allows
std::bad_exception
, std::bad_exception
is thrown.2) If std::exception_ptr stores a copy of the caught exception and if the copy constructor of the exception object caught by current_exception throws an exception, the captured exception is an instance of
std::bad_exception
.Inheritance diagram
Contents |
Member functions
constructs the bad_exception object (public member function) | |
copies the object (public member function) | |
[virtual] |
returns the explanatory string (virtual public member function) |
Inherited from std::exception
Member functions
[virtual] |
destroys the exception object (virtual public member function of std::exception )
|
[virtual] |
returns an explanatory string (virtual public member function of std::exception )
|
Example
Run this code
#include <iostream> #include <exception> #include <stdexcept> void my_unexp() { throw; } void test() throw(std::bad_exception) { throw std::runtime_error("test"); } int main() { std::set_unexpected(my_unexp); try { test(); } catch(const std::bad_exception& e) { std::cerr << "Caught " << e.what() << '\n'; } }
Output:
Caught std::bad_exception