std::bad_exception
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Utilities library
Relational operators (deprecated in C++20)
Integer comparison functions
Swap and type operations
Common vocabulary types
Type support (basic types, RTTI)
Library feature-test macros (C++20)
(C++11)
(C++20)
(C++26)
(C++20)
Coroutine support (C++20)
Contract support (C++26)
(C++20)(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
General utilities
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
(C++20)
Diagnostics library
(until C++20*)
(C++17)(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Exception handling failures
(C++11)
bad_exception
(until C++17*)
(until C++17*)
(until C++17*)
(until C++17*)
(TM TS)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++23)
(C++23)
(C++26)
(C++26)
(C++26)
std::bad_exception
Defined in header
<exception>
class bad_exception : public exception
std::bad_exception
is the type of the exception thrown by the C++ runtime in the following situations:
- If std::exception_ptr stores a copy of the caught exception and if the copy constructor of the exception object caught by std::current_exception throws an exception, the captured exception is an instance of
std::bad_exception
.
- 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.
std-bad exception-inheritance.svg
Inheritance diagram
All member functions of std::bad_exception
are constexpr.
Contents
[edit] Member functions
Inherited from std::exception
Member functions
[edit] Notes
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_constexpr_exceptions |
202411L |
(C++26) | constexpr for exception types |
[edit] Example
Compiles only in C++14 or earlier modes (may issue warnings).
Run this code
#include <exception> #include <iostream> #include <stdexcept> void my_unexp() { throw; } void test() throw(std::bad_exception) // Dynamic exception specifications // are deprecated in C++11 { throw std::runtime_error ("test"); } int main() { std::set_unexpected (my_unexp); // Deprecated in C++11, removed in C++17 try { test(); } catch (const std::bad_exception& e) { std::cerr << "Caught " << e.what() << '\n'; } }
Possible output:
Caught std::bad_exception