Click here to Skip to main content
12,633,390 members (32,265 online)
Click here to Skip to main content

Stats

905.6K views
5.7K downloads
565 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++.
#ifndef _INCLUDED_EXCPT_INFO_H_
#define _INCLUDED_EXCPT_INFO_H_

#ifndef _INCLUDED_TYPEINFO_
#define _INCLUDED_TYPEINFO_
#include <typeinfo>
#endif

#ifndef _INCLUDED_VECTOR_
#define _INCLUDED_VECTOR_
#include <vector>
#endif

#ifndef _INCLUDED_WINDOWS_H_
#define _INCLUDED_WINDOWS_H_
#include <windows.h>
#endif

namespace my_handler
{
    //All 'u..' fields of the structures below may be reserved
    //I am not aware of their intent. They are not used by the 
    //handler.

    struct etype_info;    //extended type_info
    struct etypeinfo_table;

    struct excpt_info
    {
        DWORD   u1;
        DWORD   destructor;
        DWORD   u2;
        etypeinfo_table *ptt;
    };

    struct etypeinfo_table
    {
        DWORD       count; 
        etype_info   *arr[1];
    };

    struct etype_info
    {
        DWORD           u1;
        std::type_info *ti; 
        DWORD           u2[3];
        DWORD           size;
        DWORD           copy_constructor;

        bool equal(const etype_info* t)
        {
            if(t->ti == 0 || ti == 0)
                return (t->ti == ti) ? true: false;
            //VC++ 6.0 returns type int and not bool
            return (*t->ti == *ti) ? true : false;
        }
    };

    struct catchblock;
    struct EXCEPTION_REGISTRATION;

    typedef std::vector<const etype_info*> TYPEINFOVEC;
    extern const etype_info g_catch_all_ti; //etype_info for catch(...)

    class exception_helper
    {
    public:
        static void get_all_etypeinfos(
            const excpt_info*, 
            std::vector<const etype_info*>*
            ) throw();

        //destroy the exception 
        static void destroy(
            void *pexc,
            const excpt_info* pexc_info
            ) throw();

        //---------------------------------------------------------------
		//pass exception to the catchblock by value or reference depending 
        //upon the information contained in catch_block structure. 
        //  pexc  - pointer to the exception to pass to the catch block
        //  ti    - contains exception's copy constructor or size
        //  establisher_frame - stack frame where exception should be 
        //          copied. It is the stack frame of the function in 
        //          which catch block resides.
        //  pcatchblock - tells (offset field) where to copy the exception
        //          in the frame
		//---------------------------------------------------------------
        static void pass_exc_to_catch_block(
            void             *pexc,           
            const etype_info   *ti,   
            EXCEPTION_REGISTRATION *establisher_frame,
            const catchblock *pcatch_block 
            ) throw();
    
    private:
        static void copy_exc(
            void *pexc, 
            void *ploc,
            const etype_info* ti
            ) throw();
        
        static void copy_reference(
            void *pexc, 
            void *ploc
            ) throw();
    };
}

#endif

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...

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