17.1 Exception Handling
When exception handling is used, the parts independently developed in the program can communicate with each other regarding problems encountered during program execution and handle these problems. A part of the program can detect problems that cannot be solved in this part. This part can pass the problem to other parts that are prepared to handle the problem.
Through exceptions, we can separate the problem detection from the problem resolution, so that the program's problem detection part does not have to understand how to handle the problem.
17.1.1 throw an exception of the class type
An exception is thrown by throwing a throw object. The type of the object determines which processing code should be activated. The selected processing code is the one in the call chain that matches the object type and is closest to the location where the exception is thrown.
An exception is thrown and captured in a way similar to passing a real parameter to a function. An exception can be an object of any type that can be passed to a non-reference parameter. This means that the object of this type must be copied.
When throw is executed, the statement following throw is not executed, but the control is transferred from throw to the matching catch. The catch can be a partial catch in the same function, you can also call another function that causes an exception directly or indirectly. Control is transferred from one place to another, which has two important meanings:
(1) exit early along the call chain function.
(2) In general, when an exception is handled, the local storage in the block that throws the exception does not exist.
Because local storage is released when exceptions are processed, the thrown objects cannot be stored locally. Instead, a special object called an exception object (exception object) is initialized using the throw expression. This object is created by throw and initialized as a copy of the thrown expression. The exception object will be passed to the corresponding catch and abolished after the exception is fully handled.
An exception object is created by copying the result of the thrown expression. The result must be of a reproducible type.
1. Exception object and inheritance
When an expression is thrown, the type of the thrown object during static compilation determines the type of the exception object.
2. Exceptions and pointers
When throwing a static type with a throw expression, it is troublesome to dereference the pointer in the throw. The result of resolving a pointer reference is an object whose type matches the pointer type. If the pointer points to a type in the inheritance level, the type of the object indicated by the pointer may be different from that of the pointer. Regardless of the actual object type, the abnormal object type matches the static type of the pointer. If the pointer is a base class type pointer to a derived class object, the object will be split and only the base class part will be thrown.
It is always wrong to throw a pointer to a local object, because it is the same as the pointer to a local object returned by the function. When a pointer is thrown, you must confirm that the object pointed to by the pointer exists when the code is processed.
If a pointer pointing to a local object is thrown and the processing code is in another function, the object to which the Pointer Points will no longer exist when the code is processed. Even if the processing code is in the same function, you must be sure that the object pointed to by the pointer exists at the catch. If the pointer points to an object in the block that exits before the catch, the partial object will be revoked before the catch.
Throwing a pointer is usually a bad idea: throwing a pointer requires that the object pointed to by the pointer exists anywhere in the corresponding processing code.
Void Method1 (){
Try
{
Throw range_error ("error ~ ");
}
Catch (exception & e)
{
Cout <e. what () <endl;
}
}
Void Method1 (){
Try
{
Throw range_error ("error ~ ");
}
Catch (exception & e)
{
Cout <e. what () <endl;
}
} Method1 (); // error ~
From xufei96's column