Motivation

Everybody sometimes need to write as robust code as possible. The simplest
way, in these cases, is to use exception handling. But most code is wrote in
C++/MFC, with C++ exception handling (e.g. CdbException). With
SEH, it is not possible to catch C++ exception, and C++ typed exception can not
catch SE selectively, because it is not typed in a way of C++. Solution with
catch(...) is not useful, because you will not know
'type' of exception. SE is possible to catch like unsigned int typed C++
exception, but this solution is not very nice.

Description of solution

I was looking for a method to handle SE and C++ exception together. I found
function

The _set_se_translator function
provides a way to handle Win32 exceptions (C structured exceptions) as C++ typed
exceptions. To allow each C exception to be handled by a C++ catch handler, first define a C exception "wrapper" class that
can be used, or derived from, in order to attribute a specific class type to a C
exception. To use this class, install a custom C exception translator function
that is called by the internal exception-handling mechanism each time a C
exception is raised. Within your translator function, you can throw any typed
exception that can be caught by a matching C++ catch
handler.

And now, it is enough to write wrapper class (I think, my class is quite
better than class provided by documentation for _set_se_translator):

How does it work?

When SE is raised, then it is called our own translator function - if it was
installed through _se_translator_function. This translator function
simply throw exception again, but now it is our wrapper class exception filled
with useful information.

Description of CSeException

CSeException class is based on CException
class provided by MFC. I overwrite some of useful methods, but it is working
same way like any other exception class based on CException
class - you can find description in documentation provided by Visual C++.

Usage

At first of all you need to install SeTranslator function,
in every thread of your application. Translator function is thread specific, it
means it is not globally set in application. In single thread application it is
possible to install it in constructor of application class (as I do in demo
program), however in multithreading application it is best place to install
translator function at the beginnings of every thread. When translator function
is installed, then it is possible to write code like this:

CSeException class is Unicode and MBCS strings compliant.
Please note, delete CSeException in catch() block,
because it is dynamically allocated by translator function.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

Comments and Discussions

When I do this in threads created using CreateThread, it does not come into the exception function. Is there something that I have to do extra? MSDN says that each thread has to register its own function. So I did it at the start of the thread procedure passed to the CreateThread.

I am in NY (not in Manhattan though) and got caught up watching the action today. It was pretty bad. One of my collegues have his son in the 8th grade. In his class alone, 5 of the kids had one of their parents killed in this tragedy.

Coming back, the problem was that the exception was occurring outside the try catch handler and so the translator was not catching that.

Thank you very much for taking the time to make a demo application and send it to me. I really appreciate that.

hi ,
i too facing thread problem .iam downloading xml file from the server
i used CreateThread() to download each file.After downloading that file an error "application error" the instruction at 0x0000000 referenced memory at 0x00000000 is comming .i tried to catch but can't....

i donot know why such error is comming and where, but when i download without using thread it is working fine ...

I am not able to use the SeException class b'cos in Main() program i am getting error in the line 'p[0] = 0;'
VC++ throws the Access violation error, b'cos of this error i am not able to test the SeException class. But however when i executed u r demo files it worked fine. what is the reason. I too created same Dialog based project.