17.1.4 re-Throw
After some corrective actions are performed, the catch may determine that the exception must be handled by a function at the upper layer of the function call chain. The catch can be throttled by rethrow) pass the exception to the upper-layer function in the function call chain. Re-throw is a throw not followed by a type or expression.
The null throw statement will throw an exception object again. It can only appear in catch or a function called from catch. If an empty throw is encountered when the processing code is not active, the terminate function is called.
Even if you re-throw an exception without specifying your own, an exception object is still passed along the chain. When an exception is thrown, the original exception object is not the catch parameter.
In general, catch can change its parameters. After the parameter is changed, if the catch throws an exception again, the changes will be transmitted only when the specifier is referenced.
Try
{
Throw range_error ("error ~ ");
}
Catch (exception & e) // e was a reference & here
{
Cout <e. what () <endl;
Throw;
}
Try
{
Throw range_error ("error ~ ");
}
Catch (exception & e) // e was a reference & here
{
Cout <e. what () <endl;
Throw;
} Try
{
Throw range_error ("error ~ ");
}
Catch (exception e) // e was a class instance here.
{
Cout <e. what () <endl;
Throw;
}
Try
{
Throw range_error ("error ~ ");
}
Catch (exception e) // e was a class instance here.
{
Cout <e. what () <endl;
Throw;
} 17.1.5 Process Code for capturing all exceptions
Except for providing specific catch clauses for each possible exception, you can use catch-all to catch all exceptions because it is impossible to know all exceptions that may be thrown. The catch clause form for capturing all exceptions is (...).
Catch clauses that catch all exceptions match any type of exceptions.
Try
{
Throw range_error ("error ~ ");
}
Catch (...)
{
Throw;
}
Try
{
Throw range_error ("error ~ ");
}
Catch (...)
{
Throw;
} If catch (...) is used in combination with other catch clauses, it must be the last one. Otherwise, any catch clause following it cannot be matched.
17.1.6 function test block and constructor
Before entering the constructor body, process the constructor initialization type. The catch clause in the constructor body cannot handle exceptions that may occur when processing the constructor initialization type.
To handle exceptions from the constructor initialization, you must compile the constructor as a function test block ). You can use the function test block to link a set of catch clauses to a function.
BaseManager (const BaseManager & I) try: p (I. p), use (I. use) {/* function body */}
Catch (...){
Throw;
}
BaseManager (const BaseManager & I) try: p (I. p), use (I. use) {/* function body */}
Catch (...){
Throw;
}
From xufei96's column