Namespaces
Variants
Views
Actions

std::current_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>
std::exception_ptr current_exception()
(since C++11)

If called during exception handling (typically, in a catch clause), captures the current exception object and creates an std::exception_ptr that holds a reference to that exception object, or to a copy of that exception object (it is implementation-defined if a copy is made)

If the implementation of this function requires a call to new and the call fails, the returned pointer will hold a reference to an instance of std::bad_alloc

If the implementation of this function requires to copy the captured exception object and its copy constructor throws an exception, the returned pointer will hold a reference to the exception thrown. If the copy constructor of the thrown exception object also throws, the returned pointer may hold a reference to an instance of std::bad_exception to break the endless loop.

If the function is called when no exception is being handled, an empty std::exception_ptr is returned.

Contents

[edit] Parameters

(none)

[edit] Return value

An instance of std::exception_ptr holding a reference to the exception object, or a copy of the exception object, or to an instance of std::bad_alloc or to an instance of std::bad_exception.

[edit] Exceptions

noexcept specification:  
noexcept
  (since C++11)

[edit] Example

#include <iostream>
#include <string>
#include <exception>
#include <stdexcept>
 
void handle_eptr(std::exception_ptr eptr) // passing by value is ok
{
    try {
        if (eptr != std::exception_ptr()) {
            std::rethrow_exception(eptr);
        }
    } catch(const std::exception& e) {
        std::cout << "Caught exception \"" << e.what() << "\"\n";
    }
}
 
int main()
{
    std::exception_ptr eptr;
    try {
        std::string().at(1); // this generates an std::out_of_range
    } catch(...) {
        eptr = std::current_exception(); // capture
    }
    handle_eptr(eptr);
} // destructor for std::out_of_range called here, when the eptr is destructed

Output:

Caught exception "basic_string::at"

[edit] See also

shared pointer type for handling exception objects
(typedef) [edit]
throws the exception from an std::exception_ptr
(function) [edit]
creates an std::exception_ptr from an exception object
(function template) [edit]