473,489 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

try-catch won't catch in the Release mode

In a default MFC - dialog based application (VC6.0 or VC7.1), the following
try-catch code does not be catched in Release mode, but it is OK for Debug
mode. Any compiler/link option is required to turn on or turn off?

try
{
int *i = 0;
*i = 0;
}
catch(...)
{
MessageBox("Catched");
}

Thanks in advance,
lauch
Nov 17 '05 #1
4 3010
"lauch2" <la****@discussions.microsoft.com> wrote in message
news:C3**********************************@microsof t.com...
In a default MFC - dialog based application (VC6.0 or VC7.1), the
following
try-catch code does not be catched in Release mode, but it is OK for Debug
mode. Any compiler/link option is required to turn on or turn off?

try
{
int *i = 0;
*i = 0;
}
catch(...)
{
MessageBox("Catched");
}

Thanks in advance,


The problem is that you are confusing structured exceptions and C++ typed
exceptions.

C++ typed exceptions use try and catch.

Win32 structured exceptions, of which division by zero is one, use __try and
__except.

What is happening is that the optimizing sees no possible way that a C++
exception can be thrown in your snippet and so it simply removes it (or
optimizes it away or elides it).

If you link with compile with the /EHa option you inform the compiler that
exceptions can come out of nowhere. The try, catch machinery will not be
removed. And due to what many consider a defect in the compiler, the
structured divide by zero exception will get caught in your catch-all
handler.

If you really need to do this, and I my estimation it is necessary in
development and often unwise otherwise, you should use _set_se_translator()
to translate a structured exception to a typed exception.

By the way, VS2005 does not confuse typed and structured exceptions.

Regards,
Will
Nov 17 '05 #2
thanks William,
What is happening is that the optimizing sees no possible way that a C++
exception can be thrown in your snippet and so it simply removes it (or
optimizes it away or elides it).
Do you mean that it just needs to remove the complier option /O2 in a
default setting for the release build?

thanks
lauch,

"William DePalo [MVP VC++ ]" wrote:
"lauch2" <la****@discussions.microsoft.com> wrote in message
news:C3**********************************@microsof t.com...
In a default MFC - dialog based application (VC6.0 or VC7.1), the
following
try-catch code does not be catched in Release mode, but it is OK for Debug
mode. Any compiler/link option is required to turn on or turn off?

try
{
int *i = 0;
*i = 0;
}
catch(...)
{
MessageBox("Catched");
}

Thanks in advance,


The problem is that you are confusing structured exceptions and C++ typed
exceptions.

C++ typed exceptions use try and catch.

Win32 structured exceptions, of which division by zero is one, use __try and
__except.

What is happening is that the optimizing sees no possible way that a C++
exception can be thrown in your snippet and so it simply removes it (or
optimizes it away or elides it).

If you link with compile with the /EHa option you inform the compiler that
exceptions can come out of nowhere. The try, catch machinery will not be
removed. And due to what many consider a defect in the compiler, the
structured divide by zero exception will get caught in your catch-all
handler.

If you really need to do this, and I my estimation it is necessary in
development and often unwise otherwise, you should use _set_se_translator()
to translate a structured exception to a typed exception.

By the way, VS2005 does not confuse typed and structured exceptions.

Regards,
Will

Nov 17 '05 #3
lauch2 wrote:
thanks William,
What is happening is that the optimizing sees no possible way that a
C++ exception can be thrown in your snippet and so it simply removes
it (or optimizes it away or elides it).


Do you mean that it just needs to remove the complier option /O2 in a
default setting for the release build?


Bottom line: Don't expect to catch platform exceptions (e.g. null
reference, divide by zero) using a C++ catch statement - it simply won't
work in all cases.

If you do need to handle platform exceptions in C++ code, you MUST compile
with /EHa, and you SHOULD use __set_se_translator to convert platform
exceptions to C++ exceptions.

Any other combination is simply not guaranteed to work. Ever.

Note that __set_se_translator needs to be called in each thread that wants
to have exceptions translated. Also note that the translator routine you
supply may be called many time during the propagation of a single platform
exception.

-cd
Nov 17 '05 #4
"lauch2" <la****@discussions.microsoft.com> wrote in message
news:E1**********************************@microsof t.com...
thanks William,


You are welcome.
What is happening is that the optimizing sees no possible way that a C++
exception can be thrown in your snippet and so it simply removes it (or
optimizes it away or elides it).


Do you mean that it just needs to remove the complier option /O2 in a
default setting for the release build?


No. The difference in behavior that you experience between the release and
debug builds does stem from the fact that the compiler removes the guard
around the protected section and the exception handler as an optimization.
But it would be akin to throwing the baby out with the bath water if you
were to remove optimizations in a release build to get the debug exception
handling behavior.

As Carl has already pointed out, if you must comingle structured and typed
exceptions then you must use _set_se_translator(). I can post an example
that does that if you need help.

But you must realize that catching the exception is no panacea. It is
possible that a program's state has been so trashed by the time that a
structured execption gets thrown that it is better, for example, to report
the error, create a minidump and exit.

IMO, in development, _set_se_translator() is useful, especially so since the
exception record for structured exceptions includes the address of the bad
code. But later, you really should not confuse structured exceptions which
are almost always evidence of serious programming errors and C++ typed
exceptions which are almost always more benign.

Regards,
Will
Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

39
6006
by: Erlend Fuglum | last post by:
Hi everyone, I'm having some trouble sorting lists. I suspect this might have something to do with locale settings and/or character encoding/unicode. Consider the following example, text...
13
11944
by: KefX | last post by:
This may have been discussed before, but I'm kind of confused as to why Python doesn't support having both an except ~and~ a finally clause, like this: try: raise RuntimeException except:...
7
9713
by: Robert Brewer | last post by:
Alex Martelli wrote in another thread: > One sign that somebody has moved from "Python newbie" to "good Python > programmer" is exactly the moment they realize why it's wrong to code: > > ...
9
3794
by: David Stockwell | last post by:
In referring to my copy of the python bible, it tells me I can't use all three items 'try' except and finally. I can use the t/f or t/e combinations though What combination can i use if i want...
26
2480
by: djw | last post by:
Hi, Folks- I have a question regarding the "proper" use of try: finally:... Consider some code like this: d = Device.open() try: d.someMethodThatCanRaiseError(...) if SomeCondition: raise...
6
3371
by: William Park | last post by:
(crossposted to comp.lang.python, because this may be of interest to them.) Python has try-block, within which you can raise exception. Once it's raised, execution breaks out of the try-block...
1
1753
by: djw | last post by:
c.l.p- I am having trouble understanding how one is supposed to correctly utilize try:...except:...finally: in real code. If I have a block of code like: def foo(): try: ... some code that...
40
2983
by: Steve Juranich | last post by:
I know that this topic has the potential for blowing up in my face, but I can't help asking. I've been using Python since 1.5.1, so I'm not what you'd call a "n00b". I dutifully evangelize on the...
4
2423
by: wk6pack | last post by:
Hi, I was wondering why when I declare the dim variable outside the try statement, I could use the .dispose() function but when I declare it inside the try statement, I get Name 'varname' is not...
20
3887
by: John Salerno | last post by:
I'm starting out with this: try: if int(text) 0: return True else: self.error_message() return False except ValueError: self.error_message()
0
7108
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7142
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7181
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7352
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3078
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1383
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.