Chapter 3 tools for large programs (3)

Source: Internet
Author: User

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
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.