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, if
statement and switch
statement
- 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 for
statement, which can iterate over all elements of a container or other sequence, and its syntax is of the form
for(declaration: expression)
statement
expression
begin
Represents a sequence, that is, has a sum end
member that returns an iterator
- a list of initial values enclosed in curly braces
- array
vector
orstring
other 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 usesthrow
expressions to raise exceptionstry语句块(try block)
: The exception handling part usestry
statement blocks to handle exceptions,try
throw exceptions, andcatch
catch exceptions一套异常类
: Used to pass exception-specific information betweenthrow
expressions 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"
}
try
After 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 catch
clause to handle the exception terminate
.
Standard exception
The exception classes defined by the C++ standard library are as follows
exception
Header file: defines the most common exception classexception
, only reports the occurrence of the exception, without providing any additional informationstdexcept
Header file: defines several commonly used exception classesnew
Header file: defines thebad_alloc
exception typetype_info
Header file: defines thebad_cast
exception type
exception
,bad_alloc
andbad_cast
objects 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
string
exception object should be initialized with an object or a C-style string - The exception type only defines a
what
member function, without parameters, the return value isconst 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