Click here to Skip to main content
12,349,778 members (24,900 online)
Click here to Skip to main content

Stats

886K views
5.7K downloads
562 bookmarked
Posted

How a C++ compiler implements exception handling

, 15 Apr 2002 CPOL
An indepth discussion of how VC++ implements exception handling. Source code includes exception handling library for VC++.
#include "funcinfo.h"

namespace my_handler
{
    //--------------------------------------------------------------------
    // Finds if there is any catch block in the function that is interested 
    // in handling the exception.
    //---------------------------------------------------------------------
    bool find_catch_block(
            const funcinfo *pfuncinfo,
            TYPEINFOVEC& exc_types,
            int   id,
            const tryblock **pptryblock,
            const catchblock **ppcatchblock,
            TYPEINFOVEC::iterator *piter
        ) throw()
    {
        typedef std::vector<const tryblock*> TRYVEC;
        TRYVEC vec;
        vec.reserve(5);
        get_try_block(pfuncinfo, id, vec);
        TRYVEC::iterator p;
        TRYVEC::iterator end = vec.end();
        p = std::find_if(vec.begin(), end, tryblock_find(
                              exc_types, ppcatchblock, piter));
        if(p != end)
            *pptryblock = *p;
        return (p != end);
    }

    //---------------------------------------------------------------------
    // Get pointer to all the try blocks to which the current id belongs.
    // In case of nested try blocks, the id may be in multiple try blocks.
    //---------------------------------------------------------------------
    void get_try_block(
        const funcinfo *pfuncinfo,
        int id,
        /*out*/std::vector<const tryblock*>& vec
        ) throw()
    {
        int count;
        const tryblock *ptryblock = 0;
        get_tryblock_table(pfuncinfo, &ptryblock, &count);
        for(int i = 0; i<count; ++i)
        {
            if(id>=ptryblock[i].start_id && id<=ptryblock[i].end_id)
                vec.push_back(&ptryblock[i]);
        }
    }
    
    //-----------------------------------------------------------------
    //does id have enclosing try block?
    //-----------------------------------------------------------------
    bool id_in_any_try_block(const funcinfo *pfuncinfo, int id) throw()
    {
        int count;
        const tryblock *ptryblock = 0;
        get_tryblock_table(pfuncinfo, &ptryblock, &count);
        for(int i = 0; i<count; ++i)
        {
            if(id>=ptryblock[i].start_id && id<=ptryblock[i].end_id)
                return true;
        }
        return false;
    }
}

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)

Share

About the Author

Vishal Kochhar
Web Developer
Canada Canada
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160621.1 | Last Updated 16 Apr 2002
Article Copyright 2002 by Vishal Kochhar
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid