Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      try block

      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
      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
       
       
       

      Anexception thrown in atry block can possibly be handled by an associated handler.

      Contents

      [edit]Syntax

      trycompound-statementhandler-seq (1)
      tryctor-initializer (optional)compound-statementhandler-seq (2)
      1) Anordinarytry block.
      2) Afunctiontry block.compound-statement must be the compound statement component of a function body.
      compound-statement - acompound statement
      handler-seq - a non-empty sequence ofhandlers
      ctor-initializer - member initializer list (forconstructors only)

      [edit]Ordinarytry block

      An ordinarytry block is astatement.

      If an exception is thrown from itscompound-statement, the exception will be matched against thehandlers in itshandler-seq :

      void f(){throw1;// NOT handled by the handler belowtry{throw2;// handled by the associated handler}catch(...){// handles the exception 2}throw3;// NOT handled by the handler above}

      [edit]Functiontry block

      A functiontry block is a special kind offunction body.

      If an exception is thrown from itscompound-statement orctor-initializer (if any), the exception will be matched against thehandlers in itshandler-seq :

      int f(bool cond){if(cond)throw1;return0;} struct X{int mem;     X()try: mem(f(true)){}catch(...){// handles the exception 1}     X(int)try{throw2;}catch(...){// handles the exception 2}};

      Exceptions thrown in destructors of objects with staticstorage duration or in constructors of objects associated withnon-block variables with static storage duration are not caught by a functiontry block on themain function.

      Exceptions thrown in destructors of objects with thread storage duration or in constructors of objects associated with non-block variables with thread storage duration are not caught by a functiontry block on the initial function of the thread.

      (since C++11)

      Flowing off the end of thecompound-statement of ahandler of a functiontry block is equivalent to flowing off the end of thecompound-statement of that functiontry block, unless the function is a constructor or destructor (see below).

      [edit]Constructor and destructortry block

      For a classC, if the function body of its constuctor or destructor definition is a functiontry block, and an exception is thrown during the initialization or destruction, respectively, ofC’s subobjects, the exception will also be matched against thehandlers in thehandler-seq  of the functiontry block:

      int f(bool cond=true){if(cond)throw1;return0;} struct X{int mem= f();     ~X(){throw2;}}; struct Y{    X mem;     Y()try{}catch(...){// handles the exception 1}     ~Y()try{}catch(...){// handles the exception 2}};

      Referring to any non-static member or base class of an object in the handler for a functiontry block of a constructor or destructor for that object results in undefined behavior.

      If areturn statement appears in a handler of the functiontry block of a constructor, the program is ill-formed.

      Thecurrently handled exception is rethrown if control reaches the end of a handler of the functiontry block of a constructor or destructor.

      [edit]Control flow

      Thecompound-statement of atry block is acontrol-flow-limited statement:

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

      Ajump statement (goto,break,return,continue) can be used to transfer control out of atry block (including its handlers). When this happens, each variable declared in thetry block will be destroyed in the context that directly contains its declaration:

      try{    T1 t1;try{        T2 t2;goto label;// destroy t2 first, then t1}catch(...){// executed if an exception is thrown while destroying t2}}catch(...){// executed if an exception is thrown while destroying t1}label:;

      [edit]Keywords

      try

      [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 thecompound-statement of atry block
      prohibited
      CWG 1167C++98it was unspecified whether a functiontry block on a destructor
      will catch exceptions from a base or member destructor
      such exceptions
      are caught

      [edit]See also

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

      [8]ページ先頭

      ©2009-2025 Movatter.jp