Namespaces
Variants

std:: rethrow_exception

From cppreference.net
헤더 파일에 정의됨 <exception>
[ [ noreturn ] ] void rethrow_exception ( std:: exception_ptr p ) ;
(C++11부터)
(C++26부터 constexpr)

예외 포인터 p 가 참조하는 이전에 캡처된 예외 객체, 또는 해당 객체의 복사본을 던집니다.

복사본이 생성되는지 여부는 명시되어 있지 않습니다. 복사본이 생성되는 경우, 이를 위한 저장 공간은 명시되지 않은 방식으로 할당됩니다.

p 가 null인 경우의 동작은 정의되지 않습니다.

목차

매개변수

p - 널이 아닌 std::exception_ptr

예외

p 가 참조하는 예외 객체는 복사본이 생성되지 않는 경우입니다.

그렇지 않으면, 구현이 예외 객체를 성공적으로 복사한 경우 해당 예외 객체의 사본입니다.

그렇지 않으면, std::bad_alloc 또는 예외 객체를 복사할 때 발생하는 예외가, 각각 할당 또는 복사가 실패할 경우 발생합니다.

참고 사항

P1675R2 이전에는, P1675R2 , rethrow_exception 는 예외 객체를 복사하는 것이 허용되지 않았으며, 이는 예외 객체가 스택에 할당되는 일부 플랫폼에서 구현이 불가능했습니다.

기능 테스트 매크로 표준 기능
__cpp_lib_constexpr_exceptions 202411L (C++26) constexpr 예외 타입

예제

#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

가능한 출력:

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

참고 항목

예외 객체를 처리하기 위한 공유 포인터 타입
(typedef)
현재 예외를 std::exception_ptr 에 캡처함
(function)