Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/error/current exception"

From cppreference.com
< cpp‎ | error
(Added link back to uncaught_exceptions)
m (fmt.)
 
(6 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 
{{cpp/title|current_exception}}
 
{{cpp/title|current_exception}}
 
{{cpp/error/navbar}}
 
{{cpp/error/navbar}}
{{ddcl | header=exception | since=c++11 |
+
{{ddcl|header=exception|since=c++11|
 
std::exception_ptr current_exception() noexcept;
 
std::exception_ptr current_exception() noexcept;
 
}}
 
}}
  
If called during exception handling (typically, in a {{c|catch}} clause), captures the current exception object and creates an {{lc|std::exception_ptr}} that holds either a copy or a reference to that exception object (depending on the implementation). The referenced object remains valid at least as long as there is an {{tt|exception_ptr}} object that refers to it.
+
If called during exception handling (typically, in a {{c/core|catch}} clause), captures the current exception object and creates an {{lc|std::exception_ptr}} that holds either a copy or a reference to that exception object (depending on the implementation). The referenced object remains valid at least as long as there is an {{tt|exception_ptr}} object that refers to it.
  
If the implementation of this function requires a call to {{c|new}} and the call fails, the returned pointer will hold a reference to an instance of {{lc|std::bad_alloc}}.
+
If the implementation of this function requires a call to {{c/core|new}} and the call fails, the returned pointer will hold a reference to an instance of {{lc|std::bad_alloc}}.
  
 
If the implementation of this function requires copying 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 {{lc|std::bad_exception}} to break the endless loop.
 
If the implementation of this function requires copying 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 {{lc|std::bad_exception}} to break the endless loop.
  
 
If the function is called when no exception is being handled, an empty {{lc|std::exception_ptr}} is returned.
 
If the function is called when no exception is being handled, an empty {{lc|std::exception_ptr}} is returned.
 +
 +
This function can be called in a {{lc|std::terminate_handler}} to retrieve the exception which has provoked the invocation of {{lc|std::terminate}}.
  
 
===Parameters===
 
===Parameters===
Line 18: Line 20:
 
===Return value===
 
===Return value===
 
An instance of {{lc|std::exception_ptr}} holding a reference to the exception object, or a copy of the exception object, or to an instance of {{lc|std::bad_alloc}} or to an instance of {{lc|std::bad_exception}}.
 
An instance of {{lc|std::exception_ptr}} holding a reference to the exception object, or a copy of the exception object, or to an instance of {{lc|std::bad_alloc}} or to an instance of {{lc|std::bad_exception}}.
 +
 +
===Notes===
 +
On the implementations that follow [https://itanium-cxx-abi.github.io/cxx-abi/abi.html Itanium C++ ABI] (GCC, Clang, etc), exceptions are allocated on the heap when thrown (except for {{lc|std::bad_alloc}} in some cases), and this function simply creates the smart pointer referencing the previously-allocated object, On MSVC, exceptions are allocated on stack when thrown, and this function performs the heap allocation and copies the exception object.
 +
 +
On Windows in managed CLR environments [https://learn.microsoft.com/en-us/cpp/dotnet/exceptions-in-cpp-cli], the implementation will store a {{lc|std::bad_exception}} when the current exception is a managed exception ([https://github.com/microsoft/STL/blob/65aab97a8e75e7ba409002e518ed799006dfb285/stl/src/excptptr.cpp#L367]). Note that {{c|catch(...)}} catches also managed exceptions:
 +
{{source|1=
 +
#include <exception>
 +
 +
int main()
 +
{
 +
    try
 +
    {
 +
        throw gcnew System::Exception("Managed exception");
 +
    }
 +
    catch (...)
 +
    {
 +
        std::exception_ptr ex = std::current_exception();
 +
        try
 +
        {
 +
            std::rethrow_exception(ex);
 +
        }
 +
        catch (std::bad_exception const &)
 +
        {
 +
            // This will be printed.
 +
            std::cout << "Bad exception" << std::endl;
 +
        }
 +
    }
 +
}
 +
}}
  
 
===Example===
 
===Example===
{{include| cpp/error/example_exception_ptr}}
+
{{include|cpp/error/example_exception_ptr}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/error/dsc exception_ptr}}
+
{{dsc inc|cpp/error/dsc exception_ptr}}
{{dsc inc | cpp/error/dsc rethrow_exception}}
+
{{dsc inc|cpp/error/dsc rethrow_exception}}
{{dsc inc | cpp/error/dsc make_exception_ptr}}
+
{{dsc inc|cpp/error/dsc make_exception_ptr}}
{{dsc inc | cpp/error/dsc uncaught_exception}}
+
{{dsc inc|cpp/error/dsc uncaught_exception}}
 
{{dsc end}}
 
{{dsc end}}
  
[[de:cpp/error/current exception]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[es:cpp/error/current exception]]
+
[[fr:cpp/error/current exception]]
+
[[it:cpp/error/current exception]]
+
[[ja:cpp/error/current exception]]
+
[[pt:cpp/error/current exception]]
+
[[ru:cpp/error/current exception]]
+
[[zh:cpp/error/current exception]]
+

Latest revision as of 09:47, 9 July 2024

 
 
 
Defined in header <exception>
std::exception_ptr current_exception() noexcept;
(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 either a copy or a reference to that exception object (depending on the implementation). The referenced object remains valid at least as long as there is an exception_ptr object that refers to it.

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 copying 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.

This function can be called in a std::terminate_handler to retrieve the exception which has provoked the invocation of std::terminate.

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] Notes

On the implementations that follow Itanium C++ ABI (GCC, Clang, etc), exceptions are allocated on the heap when thrown (except for std::bad_alloc in some cases), and this function simply creates the smart pointer referencing the previously-allocated object, On MSVC, exceptions are allocated on stack when thrown, and this function performs the heap allocation and copies the exception object.

On Windows in managed CLR environments [1], the implementation will store a std::bad_exception when the current exception is a managed exception ([2]). Note that catch(...) catches also managed exceptions:

#include <exception>
 
int main()
{
    try
    {
        throw gcnew System::Exception("Managed exception");
    }
    catch (...)
    {
        std::exception_ptr ex = std::current_exception();
        try
        {
            std::rethrow_exception(ex);
        }
        catch (std::bad_exception const &)
        {
            // This will be printed.
            std::cout << "Bad exception" << std::endl;
        }
    }
}

[edit] Example

#include <exception>
#include <iostream>
#include <stdexcept>
#include <string>
 
void handle_eptr(std::exception_ptr eptr) // passing by value is OK
{
    try
    {
        if (eptr)
            std::rethrow_exception(eptr);
    }
    catch(const std::exception& e)
    {
        std::cout << "Caught exception: '" << e.what() << "'\n";
    }
}
 
int main()
{
    std::exception_ptr eptr;
 
    try
    {
        [[maybe_unused]]
        char ch = std::string().at(1); // this generates a 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

Possible output:

Caught exception: 'basic_string::at: __n (which is 1) >= this->size() (which is 0)'

[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]
(removed in C++20*)(C++17)
checks if exception handling is currently in progress
(function) [edit]