std::rethrow_exception(3)
π std::rethrow_exception β the std::rethrow_exception glow-up (from the C++ Standard Library, bestie)
okay so picture this: you caught an exception ages ago, stuffed it into a little β¨keepsake boxβ¨, and now you want it BACK. that's this function. it's std::rethrow_exception from the C++ Standard Library, and honestly? it ate. π
β¨ SYNOPSIS β¨
Defined in header <exception>
[[noreturn]] void rethrow_exception( std::exception_ptr p ); (since C++11)
so it takes an exception pointer p and it throws the previously captured exception object that p refers to β or a copy of that object. no cap. and bestie, see that [[noreturn]]? that's not a vibe suggestion, that's the whole point: this function does not come back to you. π
π important tea: it is unspecified whether a copy is made. and if a copy is made, the storage for it is allocated in an unspecified way. so don't go asking where it lives, because nobody's telling.
π and the big one: the behavior is undefined ifpis null. like, undefined. not "oops error message" undefined. undefined undefined. guard yourif (eptr)checks, bestie, i'm begging.
πΈ PARAMETERS πΈ
pβ a non-nullstd::exception_ptr. that's it. that's the whole parameter list. slay for minimalism π
π RETURN VALUE π
(none) β because it throws, babes. it never gets to the "return" part. [[noreturn]] said what it said. β¨
π EXCEPTIONS π
okay so there are three possible outcomes and they're all listed, so let's go:
- the exception object referred-to by
p, if no copy is made. π«Ά - otherwise, a copy of such exception object, if the implementation successfully copied the exception object. π¦
- otherwise,
std::bad_allocor the exception thrown when copying the exception object β if allocation fails, or if copying fails, respectively. π (yes, you can get an exception while throwing an exception. it's giving chaos.)
β NOTES β
before P1675R2, rethrow_exception was not allowed to copy the exception object β which is literally unimplementable on some platforms, bestie, because on those platforms exception objects are allocated on the stack. π so the rules had to change. history!
π§ EXAMPLE π§
go ahead, run this code π
#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)current_exceptionβ captures the current exception in astd::exception_ptr(C++11) (function)
that's the whole page, bestie, you understood it perfectly, i'm obsessed π«Άβ¨
xoxo, sourced from http://cppreference.com, version 2024.06.10, and yes the footer really does say std::rethrow_exception(3) β go off, documentation π
π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms