Namespaces
Variants

std:: exception_ptr

From cppreference.net
헤더에 정의됨 <exception>
using exception_ptr = /*unspecified*/
(C++11부터)

std::exception_ptr 는 던져지고 std::current_exception 으로 캡처된 예외 객체를 관리하는 nullable 포인터와 유사한 타입입니다. std::exception_ptr 의 인스턴스는 다른 함수(아마도 다른 스레드에 있는)로 전달될 수 있으며, 여기서 예외는 다시 던져지고 catch 절로 처리될 수 있습니다.

기본 생성된 std::exception_ptr 는 널 포인터입니다; 이것은 예외 객체를 가리키지 않습니다.

두 개의 std::exception_ptr 인스턴스는 둘 다 null이거나 둘 다 동일한 예외 객체를 가리킬 경우에만 동일하게 비교됩니다.

std::exception_ptr 는 산술 타입, 열거형, 또는 포인터 타입으로 암시적으로 변환되지 않습니다. 이는 bool 으로 상황에 따라 변환 가능하며, null인 경우 false 로 평가되고, 그렇지 않은 경우 true 로 평가됩니다.

std::exception_ptr 가 참조하는 예외 객체는, 해당 객체를 참조하는 std::exception_ptr 가 적어도 하나 이상 존재하는 한 유효합니다: std::exception_ptr 는 공유 소유권 스마트 포인터입니다(참고: 이는 일반적인 예외 객체 수명 규칙 에 추가되는 사항입니다).

std::exception_ptr NullablePointer 요구 사항을 충족합니다.

예제

#include <exception>
#include <iostream>
#include <stdexcept>
#include <string>
void handle_eptr(std::exception_ptr eptr) // 값으로 전달해도 괜찮음
{
    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); // 이 코드는 std::out_of_range를 발생시킴
    }
    catch(...)
    {
        eptr = std::current_exception(); // 예외 캡처
    }
    handle_eptr(eptr);
} // eptr이 소멸될 때 std::out_of_range의 소멸자가 여기서 호출됨

가능한 출력:

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

참고 항목

예외 객체로부터 std::exception_ptr 를 생성합니다
(함수 템플릿)
현재 예외를 std::exception_ptr 에 캡처합니다
(함수)
std::exception_ptr 로부터 예외를 다시 던집니다
(함수)