std::current_exception(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

okay so bestie πŸ’– this is std::current_exception, straight outta the C++ Standard Libary manual, and she's literally the hold my exception queen. Let's get into it ✨

🌸 NAME 🌸

std::current_exception β€” std::current_exception. That's the whole name, she's not doing too much πŸ’…


πŸ¦‹ Synopsis πŸ¦‹

Defined in header <exception>:

std::exception_ptr current_exception() noexcept;

The way this one's been around since C++11 ✨

okay so here's the tea, bestie: if called during exception handling (typically, in a catch clause), std::current_exception 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. Longevity icon behavior, no cap πŸ’…

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. 😭 not the allocation fail

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. The endless loop said not today πŸ’€

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

This function can be called in a std::terminate_handler to retrieve the exception which has provoked the invocation of std::terminate. Main character energy in the terminate handler, honestly πŸ‘‘


πŸŽ€ Parameters πŸŽ€

(none) β€” she takes no arguments. Zero. Zilch. Low-maintenance queen 🧚

🌈 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. It's giving little smart-pointer bouquet of possibilities πŸ’

πŸ”₯ 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. Different vibes, same API ✨

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;
        }
    }
}

πŸ’œ Example πŸ’œ

// Run this code β€” go off, bestie πŸ’…

#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
    {
        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)'

πŸ‘‘ See also πŸ‘‘

  • exception_ptr β€” shared pointer type for handling exception objects (C++11) (typedef)
  • rethrow_exception β€” throws the exception from an std::exception_ptr (C++11) (function)
  • make_exception_ptr β€” creates an std::exception_ptr from an exception object (C++11) (function template)
  • uncaught_exception β€” checks if exception handling is currently in progress (removed in C++20) (function)
  • uncaught_exceptions β€” checks if exception handling is currently in progress (C++17) (function)

mwah πŸ’‹ that's the whole page, served fresh from http://cppreference.com on 2024.06.10, and yes bestie, she's still std::current_exception(3). bye 🫢

πŸ“¦ where this page comes from