How exception works?

Source: Internet
Author: User

This is an old article written in 2013, put on Gegahost.net http://raison.gegahost.net/?p=28

February, 2013How exception works? Filed Under:c++-tags:c++ Constructor., C + + exception handling, C + + Internal-raison @ 10:23 am

(Original works by Peixu Zhu)

1. Throw Exception

    • Set the size of the exception and then call internal routine __cxa_allocate_exception to allocate memory.
    • __cxa_allocate_exception Call malloc to allocate memory. Plus 0x78 bytes buffer to install default handlers later. If allocation fails, globally lock and throw a emergency exception, then globally unlock. If allocation successes, the __cxa_allocate_exception routine Return the address of the memory block minus 0x78 bytes.
    • Set the exception data as first argument, and TypeInfo as second argument, then call internal routine __CX A_throw .
    • In __cxa_throw, the routine installs default handlers in sequence @ The additional memory provided in the first Argument. And then unwind the stack to match and call catch block by calling __unwind_raiseexception (in Libunwind).

2. Catch Block

    • Call Dyld_stub___cxa_begin_catch.
    • Perform the blocked code.
    • Call Dyld___sub_cxa_end_catch, the allocated exception object would be released.

3. Notes

    • as above, the code in catch{...} block should is strictly scoped in the block, and the exception object should be As simple as possible, no unexpected errors in the exception object would cause unexpected problems, and difficult to Loca Te or debug it.
    • In addition to catching the exceptions thrown in codes, we should also catch default exceptions like Std::bad_allo C , since the internal routine may throw such exceptions potentially.
    • It is not a suggested to throw exceptions in any constructor methods, for causing unexpected results. If we throw an exception in a constructor method, we ' ve in fact get a memory block for the object, though the memory Addre SS is not returned to calling routine, thus, the exception handler can not get the address information at all, and can not Free the allocated memory explicitly or implicitly. Since The object is not created successfully, the destructor would not be called automatically.

How exception works?

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.