Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::set_terminate

      From cppreference.com
      <cpp‎ |error
       
       
      Diagnostics library
       
      Defined in header<exception>
      (until C++11)
      (since C++11)

      Makesf the new global terminate handler function and returns the previously installedstd::terminate_handler.f shall terminate execution of the program without returning to its caller, otherwise the behavior is undefined.

      This function is thread-safe. Every call tostd::set_terminatesynchronizes-with (seestd::memory_order) subsequent calls tostd::set_terminate andstd::get_terminate.

      (since C++11)

      Contents

      [edit]Parameters

      f - pointer to function of typestd::terminate_handler, or null pointer

      [edit]Return value

      The previously-installed terminate handler, or a null pointer value if none was installed.

      [edit]Example

      Run this code
      #include <cstdlib>#include <exception>#include <iostream> int main(){    std::set_terminate([](){std::cout<<"Unhandled exception\n"<<std::flush;std::abort();});throw1;}

      Possible output:

      Unhandled exceptionbash: line 7:  7743 Aborted                 (core dumped) ./a.out

      The terminate handler will also work for launched threads, so it can be used as an alternative to wrapping the thread function with atry/catch block. In the following example, since the exception is unhandled,std::terminate will be called.

      Run this code
      #include <iostream>#include <thread> void run(){throwstd::runtime_error("Thread failure");} int main(){try{std::thread t{run};        t.join();returnEXIT_SUCCESS;}catch(conststd::exception& ex){std::cerr<<"Exception: "<< ex.what()<<'\n';}catch(...){std::cerr<<"Unknown exception caught\n";}returnEXIT_FAILURE;}

      Possible output:

      terminate called after throwing an instance of 'std::runtime_error'  what():  Thread failureAborted (core dumped)

      With the introduction of the terminate handler, the exception thrown from the non-main thread can be analyzed, and exit can be gracefully performed.

      Run this code
      #include <iostream>#include <thread> class foo{public:    foo(){std::cerr<<"foo::foo()\n";}    ~foo(){std::cerr<<"foo::~foo()\n";}}; // Static object, expecting destructor on exitfoo f; void run(){throwstd::runtime_error("Thread failure");} int main(){    std::set_terminate([](){try{std::exception_ptr eptr{std::current_exception()};if(eptr){std::rethrow_exception(eptr);}else{std::cerr<<"Exiting without exception\n";}}catch(conststd::exception& ex){std::cerr<<"Exception: "<< ex.what()<<'\n';}catch(...){std::cerr<<"Unknown exception caught\n";}std::exit(EXIT_FAILURE);}); std::thread t{run};    t.join();}

      Output:

      foo::foo()Exception: Thread failurefoo::~foo()

      [edit]See also

      function called when exception handling fails
      (function)[edit]
      obtains the current terminate_handler
      (function)[edit]
      the type of the function called bystd::terminate
      (typedef)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/error/set_terminate&oldid=173429"

      [8]ページ先頭

      ©2009-2025 Movatter.jp