std::bad_exception

From cppreference.com
< cpp‎ | error
 
 
Utilities library
Language support
Type support (basic types, RTTI)
Library feature-test macros (哋它亢++20)
Dynamic memory management
Program utilities
Coroutine support (哋它亢++20)
Variadic functions
(哋它亢++20)
(哋它亢++26)
(哋它亢++11)
(哋它亢++20)
Debugging support
(哋它亢++26)
(哋它亢++26)
Three-way comparison
(哋它亢++20)(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)   
(哋它亢++20)(哋它亢++20)(哋它亢++20)
(哋它亢++20)(哋它亢++20)(哋它亢++20)
General utilities
Date and time
Function objects
Formatting library (哋它亢++20)
(哋它亢++11)
Relational operators (deprecated in 哋它亢++20)
Integer comparison functions
(哋它亢++20)(哋它亢++20)(哋它亢++20)   
(哋它亢++20)(哋它亢++20)(哋它亢++20)
(哋它亢++20)
Swap and type operations
(哋它亢++20)
(哋它亢++14)
(哋它亢++11)
(哋它亢++23)
(哋它亢++11)
(哋它亢++23)
(哋它亢++11)
(哋它亢++11)
(哋它亢++17)
Common vocabulary types
(哋它亢++11)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++11)
(哋它亢++11)
(哋它亢++17)
(哋它亢++17)
(哋它亢++23)
Elementary string conversions
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)


 
Diagnostics library
Exception handling
(until 哋它亢++20*)(哋它亢++17)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
Exception handling failures
(哋它亢++11)
bad_exception
(until 哋它亢++17*)
(until 哋它亢++17*)
(哋它亢++11)(until 哋它亢++17*)    
(until 哋它亢++17*)
Error codes
Error codes
Exception categories
System error support
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
Assertions
Stacktrace
(哋它亢++23)
(哋它亢++23)
 
 
Defined in header <exception>
class bad_exception;

std::bad_exception is the type of the exception thrown by the 哋它亢++ 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.
(since 哋它亢++11)
  • 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.
(until 哋它亢++17)
cpp/error/exceptionstd-bad exception-inheritance.svg

Inheritance diagram

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

Compiles only in 哋它亢++14 (or earlier) mode.

#include <exception>
#include <iostream>
#include <stdexcept>
 
void my_unexp()
{
    throw;
}
 
void test()
    throw(std::bad_exception) // Dynamic exception specifications
                              // are deprecated in 哋它亢++11
{
    throw std::runtime_error("test");
}
 
int main()
{
    std::set_unexpected(my_unexp); // Deprecated in 哋它亢++11, removed in 哋它亢++17
 
    try
    {
        test();
    }
    catch (const std::bad_exception& e)
    {
        std::cerr << "Caught " << e.what() << '\n';
    }
}

Possible output:

Caught std::bad_exception