Exceptions are anomalous behaviors that exist at run time that are outside the scope of the function's normal function. Typical exceptions include losing database connections, encountering unexpected inputs, and so on. Handling anomalies can be the hardest part of designing all systems.
If your program contains code that might throw an exception, there will often be special code handling issues. For example, if the problem with the program is that the input is invalid, the exception handling section may require the user to reenter the correct data, and if a database connection is lost, an alert message is issued.
The exception handling mechanism provides support for the collaboration between exception detection and exception handling in programs. In the C + + language, exception handling includes:
The throw expression. Anomaly detection.
A try statement block. Exception handling.
A set of exception classes that are used to pass specific information about an exception between a throw expression and a related catch clause.
Throw-expression
The exception Detection section of the program throws an exception using the throw expression. The throw expression contains the keyword throw and an expression immediately following it, where the type of the expression is the type of exception thrown. The throw expression is followed by a semicolon, which forms an expression statement.
Throw Runtime_error ("Data must refer to ...");
The exception thrown is an object of type Runtime_error. Throwing an exception terminates the current function and transfers control to the code that can handle the exception.
Try statement block
try{
Program-statements
}catch (exception-declaration) {
Handler-statments
}catch (exception-declaration) {
Handler-statements
}//...
As always, variables declared within a try statement block cannot be accessed outside the block, especially within a catch clause.
function exits while looking for code to be processed
A try statement block might call a function that contains a block of another try statement, and a new try statement block might call a new function that contains another try statement block, and so on.
The process of looking for code is just the opposite of a function call chain. When an exception is thrown, the function that throws the exception is searched first. If no matching catch clause is found, terminate the function and continue looking in the function that called the function. And so on, the execution path of the program is rolled back by layer until the appropriate type of catch clause is found.
If no matching catch clause is eventually found, the program goes to the standard library function named terminate. The behavior of the function is related to the system, and in general, executing the function causes the program to exit abnormally.
If a program does not have a try statement block and an exception occurs, the system invokes the Terminate function and terminates execution of the current program.
Note: Writing code with exception security is very difficult
An exception interrupts the normal process of the program.
For some programs, simply terminate the program when an exception occurs.
But for those who want to handle the exception and continue to execute the program, you need to pay extra attention. We must always know when the anomaly occurs, how the program should ensure that the object is valid, the resource is not leaking, the program is in a reasonable state, and so on.
Standard exceptions
The C + + standard library defines a set of classes that report problems encountered by standard library functions. These exception classes can also be used in user-written programs, which are defined in 4 header files, respectively:
exception
St
Dexcept
New
Type_info
The standard library exception class defines only a few operations, including creating or copying an object of the exception type, and assigning a value to an object of the exception type.
These types of objects should be initialized with a string object or a C-style string, but the default initialization method is not allowed. When you create such an object, you must provide an initial value that contains information about the error. In addition to exception, Bad_alloc, and Bad_cast objects, it is not allowed to provide an initial value for these objects.
<stdexcept> Definition of Exception class
The most common problems of exception
Runtime_error issues that can only be detected at run time
Range_error Runtime Error: The result of the build is beyond a meaningful range of domains
Overflow_error Run-time error: Calculation overflow
Underflow_error Run-time error: Calculating underflow
Logic_error Program Logic Error
Domain_error Logic Error: The result value corresponding to the parameter does not exist
Invalid_argument logic error: Invalid parameter
Length_error logic Error: Attempting to create an object that exceeds the maximum length of the type
Out_of_range logic Error: Using a value beyond the valid range
C++try statement block and exception handling