Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Handling exceptions

      From cppreference.com
      <cpp‎ |language
       
       
      C++ language
      General topics
      Flow control
      Conditional execution statements
      Iteration statements (loops)
      Jump statements
      Functions
      Function declaration
      Lambda function expression
      inline specifier
      Dynamic exception specifications(until C++17*)
      noexcept specifier(C++11)
      Exceptions

      catch handler
      Namespaces
      Types
      Specifiers
      constexpr(C++11)
      consteval(C++20)
      constinit(C++20)
      Storage duration specifiers
      Initialization
      Expressions
      Alternative representations
      Literals
      Boolean -Integer -Floating-point
      Character -String -nullptr(C++11)
      User-defined(C++11)
      Utilities
      Attributes(C++11)
      Types
      typedef declaration
      Type alias declaration(C++11)
      Casts
      Memory allocation
      Classes
      Class-specific function properties
      Special member functions
      Templates
      Miscellaneous
       
      Expressions
      General
      Literals
      Operators
      Conversions
       
      Exceptions
      try block
      Throwing exceptions
      Handling exceptions
      Exception specification
          noexcept specification(C++11)
          dynamic specification(until C++17*)
      noexcept operator(C++11)
       

      Anexception can be handled by a handler.

      Contents

      [edit]Handler

      catch(attr (optional)type-specifier-seqdeclarator)compound-statement (1)
      catch(attr (optional)type-specifier-seqabstract-declarator (optional))compound-statement (2)
      catch(...)compound-statement (3)
      1) A handler with a named parameter.
      2) A handler with an unnamed parameter.
      3) A handler matching all kinds of exceptions.
      attr -(since C++11) any number ofattributes, applies to the parameter
      type-specifier-seq - part of a formal parameter declaration, same as in a functionparameter list
      declarator - part of a parameter declaration, same as in a functionparameter list
      abstract-declarator - part of an unnamed parameter declaration, same as in functionparameter list
      compound-statement - acompound statement


      The parameter declaration in a handler describes the type(s) of exceptions that can cause that handler to be entered.

      If the parameter is declared to have one of the following types, the program is ill-formed:

      (since C++11)
      • a pointer to an incomplete type other than (possibly cv-qualified)void
      • an lvalue reference to an incomplete type

      If the parameter is declared to have type “array ofT” or function typeT, the type is adjusted to “pointer toT”.

      A handler with parameter typeT can be abbreviated as “a handler of typeT”.

      [edit]Matching exceptions

      Eachtry block associates with a number of handlers, these handlers form a handler sequence. When an exception is thrown from atry block, the handlers in the sequence are tried in order of appearance to match the exception.

      A handler is a match for anexception object of typeE if any of the following conditions is satisfied:

      • The handler is of type “possibly cv-qualifiedT” or “lvalue reference to possibly cv-qualifiedT”, and any of the following conditions is satisfied:
      • E andT are the same type (ignoring the top-level cv-qualifiers).
      • T is an unambiguous public base class ofE.
      • The handler is of type “possibly cv-qualifiedT” orconst T& whereT is a pointer or pointer-to-member type, and any of the following conditions is satisfied:
      • E is a pointer or pointer-to-member type that can be converted toT by at least one of the following conversions:
      (since C++17)
      (since C++11)

      Thecatch(...) handler matches exceptions of any type. If present, it can only be the last handler in a handler sequence. This handler may be used to ensure that no uncaught exceptions can possibly escape from a function that offersnothrow exception guarantee.

      try{    f();}catch(conststd::overflow_error& e){}// this executes if f() throws std::overflow_error (same type rule)catch(conststd::runtime_error& e){}// this executes if f() throws std::underflow_error (base class rule)catch(conststd::exception& e){}// this executes if f() throws std::logic_error (base class rule)catch(...){}// this executes if f() throws std::string or int or any other unrelated type

      If no match is found among the handlers for atry block, the search for a matching handler continues in a dynamically surroundingtry block of the same thread(since C++11).

      If no matching handler is found,std::terminate is invoked; whether or not the stack isunwound before this invocation ofstd::terminate is implementation-defined.

      [edit]Handling exceptions

      When an exception is thrown, control is transferred to the nearest handler with a matching type; “nearest” means the handler for which the compound statement or the member initializer list (if present) following thetry keyword was most recently entered by the thread of control and not yet exited.

      [edit]Initializing the handler parameter

      The parameter declared in the parameter list (if any), of type “possibly cv-qualifiedT” or “lvalue reference to possibly cv-qualifiedT”, is initialized from theexception object, of typeE, as follows:

      • IfT is a base class ofE, the parameter iscopy-initialized from an lvalue of typeT designating the corresponding base class subobject of the exception object.
      • Otherwise, the parameter is copy-initialized from an lvalue of typeE designating the exception object.

      The lifetime of the parameter ends when the handler exits, after the destruction of any objects with automaticstorage duration initialized within the handler.

      When the parameter is declared as an object, any changes to that object will not affect the exception object.

      When the parameter is declared as a reference to an object, any changes to the referenced object are changes to the exception object and will have effect should that object be rethrown.

      [edit]Activating the handler

      A handler is consideredactive when initialization is complete for the parameter (if any) of the handler.

      Also, an implicit handler is considered active whenstd::terminate is entered due to a throw.

      A handler is no longer considered active when the handler exits.

      The exception with the most recently activated handler that is still active is called thecurrently handled exception. Such an exception can berethrown.

      [edit]Control flow

      Thecompound-statement of a handler is acontrol-flow-limited statement:

      void f(){goto label;// errortry{goto label;// error}catch(...){goto label:// OK        label:;}}

      [edit]Notes

      Stack unwinding occurs while control is transferring to a handler. When a handler becomes active, stack unwinding is already completed.

      The exception thrown by thethrow expressionthrow0 does not match a handler of pointer or pointer-to-member type.

      • throw nullptr can be used instead to throw a null pointer that matches such handlers.
      (since C++11)

      Exception objects can never have array or function types, therefore a handler of reference to array or function type is never a match for any exception object.

      It is possible to write handlers that can never be executed, for example by placing a handler for a final derived class after a handler for a corresponding unambiguous public base class:

      try{    f();}catch(conststd::exception& e){}// will be executed if f() throws std::runtime_errorcatch(conststd::runtime_error& e){}// dead code!

      Many implementations overly extend the resolution ofCWG issue 388 to handlers of reference to non-const pointer types:

      int i;try{try{throwstatic_cast<float*>(nullptr);}catch(void*& pv){        pv=&i;throw;}}catch(constfloat* pf){assert(pf== nullptr);// should pass, but fails on MSVC and Clang}

      [edit]Keywords

      catch

      [edit]Example

      The following example demonstrates several usage cases of the handlers:

      Run this code
      #include <iostream>#include <vector> int main(){try{std::cout<<"Throwing an integer exception...\n";throw42;}catch(int i){std::cout<<" the integer exception was caught, with value: "<< i<<'\n';} try{std::cout<<"Creating a vector of size 5...\n";std::vector<int> v(5);std::cout<<"Accessing the 11th element of the vector...\n";std::cout<< v.at(10);// vector::at() throws std::out_of_range}catch(conststd::exception& e)// caught by reference to base{std::cout<<" a standard exception was caught, with message: '"<< e.what()<<"'\n";}}

      Possible output:

      Throwing an integer exception... the integer exception was caught, with value: 42Creating a vector of size 5...Accessing the 11th element of the vector... a standard exception was caught, with message: 'out_of_range'

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      CWG 98C++98aswitch statement can transfer control into a handlerprohibited
      CWG 210C++98throw expressions were matched against the handlersexception objects are
      matched against the handlers
      CWG 388C++98an exception of pointer or pointer to member type could
      not be matched by a const reference to a different type
      made matchable
      when convertible
      CWG 1166C++98the behavior was unspecified when a handler whose
      type is a reference to an abstract class type is matched
      abstract class types are
      not allowed for handlers
      CWG 1769C++98when the type of the handler is a base of the type of
      the exception object, a converting constructor might
      be used for the initialization of the handler parameter
      the parameter is copy-initialized
      from the corresponding base class
      subobject of the exception object
      CWG 2093C++98an exception object of pointer to object type could not match a
      handler of pointer to object type through qualification conversion
      allowed

      [edit]References

      • C++23 standard (ISO/IEC 14882:2024):
      • 14.4 Handling an exception [except.handle]
      • C++20 standard (ISO/IEC 14882:2020):
      • 14.4 Handling an exception [except.handle]
      • C++17 standard (ISO/IEC 14882:2017):
      • 18.3 Handling an exception [except.handle]
      • C++14 standard (ISO/IEC 14882:2014):
      • 15.3 Handling an exception [except.handle]
      • C++11 standard (ISO/IEC 14882:2011):
      • 15.3 Handling an exception [except.handle]
      • C++03 standard (ISO/IEC 14882:2003):
      • 15.3 Handling an exception [except.handle]
      • C++98 standard (ISO/IEC 14882:1998):
      • 15.3 Handling an exception [except.handle]

      [edit]See also

      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/catch&oldid=176679"

      [8]ページ先頭

      ©2009-2025 Movatter.jp