Re: Exception Logistics
I still want input to previous post, but I've been
fooling around with this little app to see which
catchs are caught and when. But I have one question
on it, was does the dtor get called twice in a row on
the unwind ?
----Entire app code with program output below----
#include <iostream.h>
#include <iomanip>
#include <windows.h>
const DWORD CPP_EXCEPTION = 0xE06D7363;
extern struct EHExceptionRecord* _pCurrentException; //CRT struct
const EXCEPTION_RECORD* GetCurrentExceptionRecord()
{
return (EXCEPTION_RECORD *)_pCurrentException;
}
class ThroObj
{
public:
unsigned long ObjVar;
unsigned long FS_0;
ThroObj() : ObjVar(0xAAAAAAAA)
{ cout << dec << "In ThrowObj ctor\n"; }
~ThroObj()
{ cout << dec << "In ThrowObj dtor\n"; }
void ObjFunc()
{
cout << dec << "In ObjFunc\n";
ObjVar = 0xBBBBBBBB;
}
};
void GlobalFunc()
{
try
{
// int Foo, Bar = 0; // Not using on this compile
// Foo = 1 / Bar;
cout << dec << "In 2nd -try block- (in GlobalFunc) fixing to throw ThrowObj\n";
throw ThroObj();
}
catch(...)
{
const EXCEPTION_RECORD* pr = GetCurrentExceptionRecord();
if (pr->ExceptionCode == CPP_EXCEPTION)
{
cout << dec << "In catch(...), and SEH Win32 Exception with code "
<< hex << pr->ExceptionCode << dec << "\noccured at addr "
<< hex << pr->ExceptionAddress << "\n"
<< dec << "fixing to re- throw ThroObj again\n";
throw ThroObj();
}
else if (pr->ExceptionCode != CPP_EXCEPTION)
{
cout << "In catch(...), and SEH Win32 Exception with code " << hex
<< pr->ExceptionCode << dec << "\noccured at addr "
<< hex << pr->ExceptionAddress << "\n"
<< dec << "fixing to re- throw ThroObj again\n";
throw ThroObj();
}
}
}
void main(void)
{
try
{ cout << "In 1st -try block- calling GlobalFunc) \n";
GlobalFunc();
}
catch( ThroObj E )
{
const EXCEPTION_RECORD* pr = GetCurrentExceptionRecord();
if (pr->ExceptionCode == CPP_EXCEPTION)
{
cout << dec << "In outer Catch(ThrowObj E)\n"
<< dec << "Exception code "
<< hex << pr->ExceptionCode << dec << "\noccured at addr "
<< hex << pr->ExceptionAddress << "\n";
}
else if (pr->ExceptionCode != CPP_EXCEPTION)
{
cout << dec << "In outer Catch(ThrowObj E)\n"
<< dec << "Exception code "
<< hex << pr->ExceptionCode << dec << "\noccured at addr "
<< hex << pr->ExceptionAddress << "\n";
}
E.ObjFunc();
}
}
// on the output I see the same First chance exception I always get on my
// debugger output window. I surmise this is because of the debug compile
// has optimizer off (using VC 6 )
First-chance exception in ES1c.exe (KERNEL32.DLL): 0xE06D7363:
/* program output |
In 1st -try block- calling GlobalFunc) |
In 2nd -try block- (in GlobalFunc) fixing to throw ThrowObj |
In ThrowObj ctor |
In ThrowObj dtor |
In catch(...), and SEH Win32 Exception with code e06d7363 <--+
occured at addr 0x7C812AFB
fixing to re- throw ThroObj again
In ThrowObj ctor
In ThrowObj dtor
In ThrowObj dtor <---------------dtor called twice ??
In outer Catch(ThrowObj E)
Exception code e06d7363
occured at addr 0x7C812AFB
In ObjFunc
In ThrowObj dtor
In ThrowObj dtor
end output */