std::bad_exception

From cppreference.com
 
 
 
Error handling
Exception handling
exception
uncaught_exception
exception_ptr (C++11)
make_exception_ptr (C++11)
current_exception (C++11)
rethrow_exception (C++11)
nested_exception (C++11)
throw_with_nested (C++11)
rethrow_if_nested (C++11)
Exception handling failures
terminate
terminate_handler
get_terminate (C++11)
set_terminate
unexpected (deprecated)
bad_exception
unexpected_handler (deprecated)
get_unexpected (C++11)(deprecated)
set_unexpected (deprecated)
Exception categories
logic_error
invalid_argument
domain_error
length_error
out_of_range
runtime_error
range_error
overflow_error
underflow_error
Error codes
Error codes
errno
Assertions
assert
system_error facility
error_category (C++11)
generic_category (C++11)
system_category (C++11)
error_condition (C++11)
errc (C++11)
error_code (C++11)
system_error (C++11)
 
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.
cpp/error/exceptionstd-bad exception-inheritance.svg
About this image

Inheritance diagram

Contents

[edit] 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]
destructs the exception object
(virtual public member function of std::exception)
[virtual]
returns an explanatory string
(virtual public member function of std::exception)

[edit] Example

#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