Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / C++

How a C++ compiler implements exception handling

Rate me:
Please Sign up or sign in to vote.
4.96/5 (196 votes)
15 Apr 2002CPOL26 min read 1.1M   6.2K   579  
An indepth discussion of how VC++ implements exception handling. Source code includes exception handling library for VC++.
#include "funcinfo.h"
#include "sehhelper.h"

namespace my_handler
{
    //---------------------------------------------------------------
    // Destroy local objects of a block or a function. 
    //---------------------------------------------------------------
    void local_unwind(
        const unwind *punwind_table,
        EXCEPTION_REGISTRATION *pFrame,
        const int last_id,
        const int new_id
        ) throw()
    {
        //EBP of the function whose stack is being unwound
        DWORD _ebp = reinterpret_cast<DWORD>(&pFrame->ebp);
        destroy_local_objects(_ebp, punwind_table, pFrame->id, last_id);
        pFrame->id = new_id;
    }

    void destroy_local_objects(
        const DWORD _ebp,
        const unwind *punwind_table,
        const int cur_id, 
        const int last_id
        ) throw()
    {
        INSTALL_TERMINATION_HANDLER
        _destroy_local_objects(_ebp, punwind_table, cur_id, last_id);
        REMOVE_TERMINATION_HANDLER
    }

    //--------------------------------------------------------------------
    // _ebp    - Contains the value of the stack frame pointer (EBP) for 
    //           the function whose local objects are being destroyed.
    // cur_id  - The current id on the stack frame
    // last_id - When the prev field of the unwind linked list equals 
    //           last_id, it should not inspect the prev unwind structure
    //           for destroying the object and should return. 
    //--------------------------------------------------------------------
    void _destroy_local_objects(
        const DWORD _ebp,
        const unwind *punwind_table, 
        const int cur_id, 
        const int last_id
        ) throw()
    {
        int id = cur_id;
        while(id != last_id && id>=0)
        {
            CLEANUP_FUNC cf = punwind_table[id].cf;
            if(cf != 0) 
            {
                _asm
                {
                    mov EAX, cf
                    push EBP       //save current EBP
                    mov EBP, _ebp  //the EBP of the unwinding function
                    call EAX
                    pop EBP
                }
            }
            id = punwind_table[id].prev;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions