Re: Exception Handling in Release Mode
{ Accepted as follow-up in thread, but please consider clc++m topicality
in further follow-ups (see link in clc++m banner at end). -mod/aps }
On Feb 15, 10:49 pm, "Moahn" <ramamo...@rediffmail.com> wrote:
It is working fine in Win32 Debug build, but it is not catching the
exception in Win32Release mode.
int main(int argc, char* argv[])
{
int i = 0;
try
{
int m = 17/i;
}
catch(...)
{
cout<<"Main - Catch"<<endl;
}
cout <<"End of Main" <<endl;
return 0;
}
Could you please explain, why the Exception is not caught in the
Win32Release mode.??
Well, it looks like nobody gave you full explanation of what's going
on here. Basically, in C++ Win32 world on x86 platform there are:
- Structured exceptions -- "hard" errors, usually 'divide by zero' or
'access violation'; they are usually generated by processor when it
stumbles upon related condition (read MSDN about __try/__except)
- C++ exceptions -- these basically are glorified function calls (you
could think of 'throw' as a 'call special function')
Your program generates SE. Ability to catch SE with 'catch(...)' -- is
the very bad idea implemented long ago, MS struggled to drop this
"feature" for quite a while since then. And your example is just one
of many that show why it should not be like this. Another, more
illustrating, is:
int i = 0;
try
{
MyClass mc;
int m = 17/i;
}
catch(...)
{
}
Outcome: mc's destructor won't ever be called.
Now, why it does not get caught in Release mode: because optimizer
kicks in. From his point of view try/catch clause is unnecessary (no
function calls, no 'throw' statements) -- it simply optimizes it away.
But if you really want compiler to treat every operation as one that
could throw -- use asynchronous exception model (see MSDN). It could
slow your code performance considerably though... According to rumors
SQL2k's engine is compiled with this option. ;-)
Oh, and yes -- get yourself better compiler (gcc or latest MS VC), it
is bad to learn C++ using seriously flawed C++ compiler -- it is like
studying microbiology using broken microscope
Bye.
Sincerely yours, Michael.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]