Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::rethrow_exception

      From cppreference.com
      <cpp‎ |error
       
       
      Diagnostics library
       
      Defined in header<exception>
      [[noreturn]]void rethrow_exception(std::exception_ptr p);
      (since C++11)
      (constexpr since C++26)

      Throws the previously captured exception object referred-to by the exception pointerp, or a copy of that object.

      It is unspecified whether a copy is made. If a copy is made, the storage for it is allocated in an unspecified way.

      The behavior is undefined ifp is null.

      Contents

      [edit]Parameters

      p - non-nullstd::exception_ptr

      [edit]Exceptions

      The exception object referred-to byp if no copy is made.

      Otherwise, a copy of such exception object if the implementation successfully copied the exception object.

      Otherwise,std::bad_alloc or the exception thrown when copying the exception object, if allocation or copying fails, respectively.

      [edit]Notes

      BeforeP1675R2,rethrow_exception was not allowed to copy the exception object, which is unimplementable on some platforms where exception objects are allocated on the stack.

      Feature-test macroValueStdFeature
      __cpp_lib_constexpr_exceptions202411L(C++26)constexpr for exception types

      [edit]Example

      [edit]
      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(conststd::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

      Possible output:

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

      [edit]See also

      shared pointer type for handling exception objects
      (typedef)[edit]
      captures the current exception in astd::exception_ptr
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/error/rethrow_exception&oldid=177870"

      [8]ページ先頭

      ©2009-2025 Movatter.jp