Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::current_exception

      From cppreference.com
      <cpp‎ |error
       
       
      Diagnostics library
       
      Defined in header<exception>
      std::exception_ptr current_exception()noexcept;
      (since C++11)
      (constexpr since C++26)

      If called during exception handling (typically, in acatch clause), captures the current exception object and creates anstd::exception_ptr that holds either a copy or a reference to that exception object (depending on the implementation). The referenced object remains valid at least as long as there is anexception_ptr object that refers to it.

      If the implementation of this function requires a call tonew and the call fails, the returned pointer will hold a reference to an instance ofstd::bad_alloc.

      If the implementation of this function requires copying the captured exception object and its copy constructor throws an exception, the returned pointer will hold a reference to the exception thrown. If the copy constructor of the thrown exception object also throws, the returned pointer may hold a reference to an instance ofstd::bad_exception to break the endless loop.

      If the function is called when no exception is being handled, an emptystd::exception_ptr is returned.

      This function can be called in astd::terminate_handler to retrieve the exception which has provoked the invocation ofstd::terminate.

      Contents

      [edit]Return value

      An instance ofstd::exception_ptr holding a reference to the exception object, or a copy of the exception object, or to an instance ofstd::bad_alloc or to an instance ofstd::bad_exception.

      [edit]Notes

      On the implementations that followItanium C++ ABI (GCC, Clang, etc), exceptions are allocated on the heap when thrown (except forstd::bad_alloc in some cases), and this function simply creates the smart pointer referencing the previously-allocated object, On MSVC, exceptions are allocated on stack when thrown, and this function performs the heap allocation and copies the exception object.

      On Windows in managed CLR environments[1], the implementation will store astd::bad_exception when the current exception is a managed exception ([2]). Note thatcatch(...) catches also managed exceptions:

      #include <exception> int main(){try{throw gcnew System::Exception("Managed exception");}catch(...){std::exception_ptr ex= std::current_exception();try{std::rethrow_exception(ex);}catch(std::bad_exceptionconst&){// This will be printed.std::cout<<"Bad exception"<<std::endl;}}}
      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]
      throws the exception from anstd::exception_ptr
      (function)[edit]
      creates anstd::exception_ptr from an exception object
      (function template)[edit]
      (removed in C++20*)(C++17)
      checks if exception handling is currently in progress
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/error/current_exception&oldid=177869"

      [8]ページ先頭

      ©2009-2025 Movatter.jp