Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

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++.
exceptionhandler_src.zip
demo.dsp
demo.dsw
excptstorage.dsp
excptstorage.dsw
excpt_helper.exp
excpt_helper.lib
MyExcHandler.dsp
MyExcHandler.dsw
#include "excptstorage.h"
#include <windows.h>

namespace
{
    DWORD dwstorage;
}

namespace my_handler
{
    __declspec(dllexport) exception_storage* get_exception_storage() throw()
    {
        void *p = TlsGetValue(dwstorage);
        return reinterpret_cast<exception_storage*>(p);
    }
}


BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
					 )
{
    using my_handler::exception_storage;
    exception_storage *p;
    switch(ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
        //For the first main thread, this case also contains implicit 
        //DLL_THREAD_ATTACH, hence there is no DLL_THREAD_ATTACH for 
        //the first main thread.
        dwstorage = TlsAlloc();
        if(-1 == dwstorage)
            return FALSE;
        p = new exception_storage();
        TlsSetValue(dwstorage, p);
        break;
    case DLL_THREAD_ATTACH:
        p = new exception_storage();
        TlsSetValue(dwstorage, p);
        break;
    case DLL_THREAD_DETACH:
        p = my_handler::get_exception_storage();
        delete p;
        break;
    case DLL_PROCESS_DETACH:
        p = my_handler::get_exception_storage();
        delete p;
        break;
    }
    return TRUE;
}

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

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