C + + exception handling (use of Try statements, exception propagation, stack expansion)

Source: Internet
Author: User

A try block can be nested and propagated to an external capture when the try inner module is caught in an exception. When an inner layer has caught an exception, it is no longer propagated to the outer layers. When you need to propagate to the outer layer, you need to catch the exception internally and then throw the exception again, and you can propagate to the outside. Note that the copy constructor is called when the exception is re-thrown.

When the exception is not captured, the Terminate function is called, and the Terminate function terminates the process by calling the system's abort () function by default. You can use the Set_terminate function to set the function called by terminate.

The stack unwind is defined by looking up along the nested call link until a catch clause is found for the exception. This process is called stack unwinding. During stack unwinding, destructors are called for production local objects.

An exception can be thrown in the constructor, and the constructed object should be eliminated if an exception is thrown (if you have constructed an obj = new obj, make sure to delete the new object). The destructor is not called because the construct is not completed.

Note: When an exception is thrown, the destructor is called instead of catching the exception, and after the exception is thrown in main, the Terminate function is called when the destructor is called before the exception is caught and an exception is thrown. It makes no sense to throw an exception, so generally you should not throw an exception in a destructor.

C + + exception handling (use of Try statements, exception propagation, stack expansion)

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.