Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::uncaught_exception,std::uncaught_exceptions

      From cppreference.com
      <cpp‎ |error
       
       
      Diagnostics library
       
      Defined in header<exception>
      (1)
      bool uncaught_exception()throw();
      (until C++11)
      bool uncaught_exception()noexcept;
      (since C++11)
      (deprecated in C++17)
      (removed in C++20)
      int uncaught_exceptions()noexcept;
      (2)(since C++17)
      (constexpr since C++26)
      1) Detects if the current thread has a live exception object, that is, an exception has been thrown or rethrown and not yet entered a matching catch clause,std::terminate orstd::unexpected. In other words,std::uncaught_exception detects ifstack unwinding is currently in progress.
      2) Detects how many exceptions in the current thread have been thrown or rethrown and not yet entered their matching catch clauses.

      Sometimes it's safe to throw an exception even whilestd::uncaught_exception()==true(until C++17)std::uncaught_exceptions()>0(since C++17). For example, ifstack unwinding causes an object to be destructed, the destructor for that object could run code that throws an exception as long as the exception is caught by some catch block before escaping the destructor.

      Contents

      [edit]Parameters

      (none)

      [edit]Return value

      1)true if stack unwinding is currently in progress in this thread,false otherwise.
      2) The number of uncaught exception objects in the current thread.

      [edit]Notes

      An example where int-returninguncaught_exceptions is used is theboost.log library: the expressionBOOST_LOG(logger)<< foo(); first creates a guard object and records the number of uncaught exceptions in its constructor. The output is performed by the guard object's destructor unlessfoo() throws (in which case the number of uncaught exceptions in the destructor is greater than what the constructor observed).

      std::experimental::scope_fail andstd::experimental::scope_success in LFTS v3 rely on the functionality ofuncaught_exceptions, because their destructors need to do different things that depend on whether is called during stack unwinding.

      Feature-test macroValueStdFeature
      __cpp_lib_uncaught_exceptions201411L(C++17)std::uncaught_exceptions
      __cpp_lib_constexpr_exceptions202411L(C++26)constexpr for exception types

      [edit]Example

      Run this code
      #include <exception>#include <iostream>#include <stdexcept> struct Foo{char id{'?'};int count= std::uncaught_exceptions();     ~Foo(){        count== std::uncaught_exceptions()?std::cout<< id<<".~Foo() called normally\n":std::cout<< id<<".~Foo() called during stack unwinding\n";}}; int main(){    Foo f{'f'}; try{        Foo g{'g'};std::cout<<"Exception thrown\n";throwstd::runtime_error("test exception");}catch(conststd::exception& e){std::cout<<"Exception caught: "<< e.what()<<'\n';}}

      Possible output:

      Exception throwng.~Foo() called during stack unwindingException caught: test exceptionf.~Foo() called normally

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 70C++98the exception specification ofuncaught_exception() was missingspecified asthrow()

      [edit]See also

      function called when exception handling fails
      (function)[edit]
      shared pointer type for handling exception objects
      (typedef)[edit]
      captures the current exception in astd::exception_ptr
      (function)[edit]

      [edit]External links

      1. GOTW issue 47: Uncaught Exceptions
      2. Rationale forstd::uncaught_exceptions (N4125)
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/error/uncaught_exception&oldid=178563"

      [8]ページ先頭

      ©2009-2025 Movatter.jp