C++ Study Notes (5) - Statements and Exceptions

C++ Study Notes (5) - Statements and Exceptions

C++ provides a set of control flow statements to support more complex execution paths

Conditional statements

C++ language provides two conditional statements, ifstatement and switchstatement

  • Dangling else: else matches its nearest if not yet matched
  • case labels must be integer constant expressions
  • Variable definition inside switch: If you want to define and initialize a variable in a case branch, you should define the variable in a block, and ensure that all subsequent case labels are outside the scope of the variable.

iteration statement

The new C++11 standard introduces a range forstatement, which can iterate over all elements of a container or other sequence, and its syntax is of the form

for(declaration: expression)
    statement

expressionbeginRepresents a sequence, that is, has a sum endmember that returns an iterator

  • a list of initial values ​​enclosed in curly braces
  • array
  • vectoror stringother type
for(int a : {1, 2, 3})  // 初始化列表
    cout << a << endl;  

try block and exception handling

The exception handling mechanism provides support for exception detection and exception handling in programs. Exception handling in C++ includes

  • throw表达式(throw expression): The exception detection section uses throwexpressions to raise exceptions
  • try语句块(try block): The exception handling part uses trystatement blocks to handle exceptions, trythrow exceptions, and catchcatch exceptions
  • 一套异常类: Used to pass exception-specific information between throwexpressions and associated clausescatch

throw expression

  • Throw an exception

    throw runtime_error("This is the first runtime_error");

try block

try{
    // 处理代码,失败就会抛出 runtime_error 异常
    throw runtime_error("This is the second runtime_error");
}catch(runtime_error err){
    cout << err.what();  // 捕获异常,输出异常信息 "This is the second runtime_error"
}

tryAfter the statement block throws an exception, the program goes back layer by layer along the execution path of the program to find the appropriate type of catchclause to handle the exception terminate.

Standard exception

The exception classes defined by the C++ standard library are as follows

  • exceptionHeader file: defines the most common exception class exception, only reports the occurrence of the exception, without providing any additional information
  • stdexceptHeader file: defines several commonly used exception classes
  • newHeader file: defines the bad_allocexception type
  • type_infoHeader file: defines the bad_castexception type
    exception_class_defined_in_stdexcept

  • exception, bad_allocand bad_castobjects can only perform default initialization and cannot provide initial values ​​for them

  • In addition to the above, other exception types do not allow default initialization, and the stringexception object should be initialized with an object or a C-style string
  • The exception type only defines a whatmember function, without parameters, the return value is const char *, if the exception type has a string initial value, the string is returned, if there is no initial value, the return content is determined by the compiler

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325629172&siteId=291194637