std:: rethrow_exception
|
Definiert im Header
<exception>
|
||
|
[
[
noreturn
]
]
void
rethrow_exception
(
std::
exception_ptr
p
)
;
|
(seit C++11)
(constexpr seit C++26) |
|
Wirft das zuvor erfasste Ausnahmeobjekt, auf das der Ausnahmepointer p verweist, oder eine Kopie dieses Objekts.
Es ist nicht spezifiziert, ob eine Kopie erstellt wird. Falls eine Kopie erstellt wird, wird der Speicher dafür auf nicht spezifizierte Weise allokiert.
Das Verhalten ist undefiniert, wenn p null ist.
Inhaltsverzeichnis |
Parameter
| p | - | nicht-null std::exception_ptr |
Exceptions
Das Ausnahmeobjekt, auf das verwiesen wird durch p falls keine Kopie erstellt wird.
Andernfalls eine Kopie eines solchen Ausnahmeobjekts, falls die Implementierung das Ausnahmeobjekt erfolgreich kopiert hat.
Andernfalls, std::bad_alloc oder die Ausnahme, die beim Kopieren des Ausnahmeobjekts ausgelöst wird, wenn die Allokation bzw. das Kopieren fehlschlägt.
Hinweise
Vor
P1675R2
war es
rethrow_exception
nicht erlaubt, das Exception-Objekt zu kopieren, was auf einigen Plattformen, auf denen Exception-Objekte auf dem Stack allokiert werden, nicht implementierbar ist.
| Feature-Test Makro | Wert | Std | Feature |
|---|---|---|---|
__cpp_lib_constexpr_exceptions
|
202411L
|
(C++26) | constexpr für Ausnahmetypen |
Beispiel
#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
Mögliche Ausgabe:
Caught exception: 'basic_string::at: __n (which is 1) >= this->size() (which is 0)'
Siehe auch
|
(C++11)
|
Gemeinsamer Zeigertyp zur Behandlung von Ausnahmeobjekten
(typedef) |
|
(C++11)
|
Erfasst die aktuelle Ausnahme in einem
std::exception_ptr
(Funktion) |