C ++ abnormal process

Share pictures

c++ exception handling

strong>

Some abnormal situations are often encountered when the program is running, for example: the divisor is 0 when doing division; the user enters a negative number when entering the age; when the new operator is used to dynamically allocate space, the space is not enough. Unable to allocate; when accessing array elements, the subscript is out of bounds; when opening the file for reading, the file does not exist, etc.

If these abnormal situations cannot be discovered and dealt with, it may cause the program to crash.

The so-called “processing” can be to give an error message, and then let the program continue to execute along a path that does not make mistakes; it may also be that the program has to be ended, but some necessary work is done before the end , Such as writing data in memory to files, closing open files, releasing dynamically allocated memory space, etc.

C++ introduced an exception handling mechanism. The basic idea is: function A can leave it alone when it finds an exception during its execution, but just “throw an exception” to the caller of A, assuming it is function B.

Throwing an exception without handling it will cause function A to terminate immediately. In this case, function B can choose to catch the exception thrown by A for processing, or choose to ignore it. If ignored, the exception will be thrown to the caller of B, and so on.

If a layer of functions does not handle the exception, the exception will eventually be thrown to the outermost main function. The main function should handle exceptions. If the main function does not handle exceptions, the program will terminate abnormally immediately.

Basic syntax of C++ exception handling

C++ implements exception handling through throw statement and try…catch statement. The syntax of the throw statement is as follows:

Share a picture

This The statement throws an exception. An exception is an expression whose value type can be a basic type or a class.

Share a picture

There can be multiple catches, but There must be at least one.

You might as well call try and the content in the following {} as a “try block”, and catch and the content in the following {} as a “catch block”

try. The execution process of the ..catch statement is: execute the statement in the try block, if no exception is thrown during the execution, the statement after the last catch block will be executed after execution, and all the statements in the catch block will not be Execution; if an exception is thrown during the execution of the try block, then immediately after the exception is thrown, jump to the first “exception type” and the thrown exception type to match the catch block (called the exception being executed by the catch block “Capture”), and then jump to the last catch block after execution to continue execution.

For example, the following program:

Share a picture

p>

The running results of the program are as follows:

Share pictures

>

Description that when n is not 0, no exception will be thrown in the try block. Therefore, after the try block is executed normally, the program will continue execution beyond all the catch blocks, and none of the catch blocks will be executed.

The running result of the program may also be as follows:

Share a picture

When n is 0, an integer exception will be thrown in the try block. After the exception is thrown, the try block immediately stops execution. The integer exception will be caught by the first catch block that matches the type, that is, enter the catch (int e) block to execute. After the catch block is executed, the program will continue to execute until it ends normally.

If the thrown exception is not caught by the catch block, for example, change catch(int e) to catch(char e), when the input n is 0, the integer exception thrown is If there is no catch block that can be caught, the exception will not be handled, and the program will be aborted immediately, and the content after the try…catch will not be executed.

The above is the details of the c++ exception handling method

Leave a Comment

Your email address will not be published.