473,405 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

synchronous vs. asynchronous exception model

Can someone explaing the difference between these exception models regarding
the structured exception handling? The documentation is not clear. Some
code would actually help.

Thx
Nov 17 '05 #1
15 3228
Javier Estrada wrote:
Can someone explaing the difference between these exception models
regarding the structured exception handling? The documentation is
not clear. Some code would actually help.


The difference lies in assumptions the compiler makes about where exceptions
can be thrown. Under the synchronous model, only throw statements can
result in an exception being thrown. Under the asynchronous model, any
instruciton can result in an exception being thrown.

As a result, under the synchronous model, the compiler can leave out
exception handling code in areas where it can prove that no exception can be
thrown (due to the lack of throw statements), while under the async model,
all of the exception handling machinery is required all the time.

If you're writing C++ code that needs to handle native (Win32 Structured)
exceptions, you should be compiling with /EHa. If you don't, there may be
cases where objects with automatic storage (i.e. stack based) do not have
their destructors called when a native exception is raised.

-cd
Nov 17 '05 #2
And if you are compiling parts of your call tree with /clr you need to use
/EHa as well since the CLR uses SEH exceptions as its underlying mechanism.

Ronald Laeremans
Visual C++ team

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:%2****************@tk2msftngp13.phx.gbl...
Javier Estrada wrote:
Can someone explaing the difference between these exception models
regarding the structured exception handling? The documentation is
not clear. Some code would actually help.


The difference lies in assumptions the compiler makes about where
exceptions can be thrown. Under the synchronous model, only throw
statements can result in an exception being thrown. Under the
asynchronous model, any instruciton can result in an exception being
thrown.

As a result, under the synchronous model, the compiler can leave out
exception handling code in areas where it can prove that no exception can
be thrown (due to the lack of throw statements), while under the async
model, all of the exception handling machinery is required all the time.

If you're writing C++ code that needs to handle native (Win32 Structured)
exceptions, you should be compiling with /EHa. If you don't, there may be
cases where objects with automatic storage (i.e. stack based) do not have
their destructors called when a native exception is raised.

-cd

Nov 17 '05 #3
Well, it seems like it's bad both ways, isn't it.

On one hand, the assumption that only throw statements will result in an
exception is ridiculous.

I need to write C++ code to cope with SEH (e.g., accessing invalid pointers,
etc.). However, having objects with automatic storage undestroyed poses yet
another risk.

Does translating the SEH exceptions to C++ exceptions help in any way? I
just read an article on The Code Project on replacing the exceptions, but at
the end it also recommends using a workaround or the asynchronous model.

Thx


Nov 17 '05 #4
Javier Estrada wrote:
Well, it seems like it's bad both ways, isn't it.

On one hand, the assumption that only throw statements will result in
an exception is ridiculous.

I need to write C++ code to cope with SEH (e.g., accessing invalid
pointers, etc.). However, having objects with automatic storage
undestroyed poses yet another risk.
If you compile with /EHa there is no such risk.
Does translating the SEH exceptions to C++ exceptions help in any
way? I just read an article on The Code Project on replacing the
exceptions, but at the end it also recommends using a workaround or
the asynchronous model.


To translate exceptions to C++ exceptions you need to compile with /EHa.
Under VC7{.1}, you can go through the motions of converting SE's to C++
exceptions (__set_se_handler, etc), but in practice it won't work. Anywhere
the compiler optimized out the exception handling machinery, you translation
will just as effectively have been optimized out.

A couple things to be aware of when translating exceptions: First, you need
to call __set_se_handler separately in each thread. Second, there's only
one handler per thread, so if some other library has already done it, you'll
wipe out their handler. Finally, the translator is called each time a
catch() block is evaluated and an SE is being processed (it's called by the
exception filter routine, in Win32 SEH terms). If you have deeply nested
try/catch structures, you may translate a single excepiton many times before
it's handled (or not). Look for articles on Matt Peitrek and a series by
Bobby Schmidt on MSDN for lots of details into how exception handling works
if you need more info.

IMO, there's no downside to /EHa except for slightly slower code in some
cases. If you're serious about catching and dealing with structured
exceptions, or interacting with code that uses structured exceptions, you
must use /EHa. As Ronald pointed out, any interaction with the CLR requires
/EHa. In fact, in VC++ 2005 this is enforced by the compiler - adding /clr
implicitly add /EHa and will cause an error if you try to use /EHs.

-cd
Nov 17 '05 #5
Thx. Very helpful information--thus the MVP title, eh?
Nov 17 '05 #6
Ronald Laeremans [MSFT] wrote:
And if you are compiling parts of your call tree with /clr you need to use
/EHa as well since the CLR uses SEH exceptions as its underlying mechanism.


I find this rather hard to swallow. When creating a Managed C++ project,
the IDE will automatically set /clr and /EHsc. Does this mean that you
get incorrect compiler settings by default? And why do managed
exceptions seem to work in my application?

--
Gerhard Menzl

#dogma int main ()

Humans may reply by replacing the obviously faked part of my e-mail
address with "kapsch".
Nov 17 '05 #7
"Javier Estrada" <lj*******@hotmail.com> wrote in message
news:eJ**************@TK2MSFTNGP10.phx.gbl...
Thx. Very helpful information--thus the MVP title, eh?


I always thought you had to climb tall mountains and meditate with wizened
gurus to get the MVPs.. that's my illusions shattered..

Steve
Nov 17 '05 #8
"Gerhard Menzl" <ge***********@spamspamspamlovelyspam.net> wrote in message
news:41********@news.kapsch.co.at...
Ronald Laeremans [MSFT] wrote:
And if you are compiling parts of your call tree with /clr you need to
use /EHa as well since the CLR uses SEH exceptions as its underlying
mechanism.


I find this rather hard to swallow. When creating a Managed C++ project,
the IDE will automatically set /clr and /EHsc. Does this mean that you get
incorrect compiler settings by default? And why do managed exceptions seem
to work in my application?


I also didn't know about this, and it seems a rather dangerous omission. I
don't know of any problems we've had NOT using /EHa, however. There also
doesn't appear to be any option in the IDE to enable /EHa - which means that
the development team either didn't have a lot of problems not using it, or
added it programmatically to the command line but didn't add it to the IDE
for whatever reason?

Steve
Nov 17 '05 #9
Gerhard Menzl wrote:
Ronald Laeremans [MSFT] wrote:
And if you are compiling parts of your call tree with /clr you need
to use /EHa as well since the CLR uses SEH exceptions as its
underlying mechanism.


I find this rather hard to swallow. When creating a Managed C++
project, the IDE will automatically set /clr and /EHsc. Does this
mean that you get incorrect compiler settings by default? And why do
managed
exceptions seem to work in my application?


That's what it means, yes - MC++ projects created by VC++ 2003 don't handle
exceptions properly.

Unless you produce a construct in which an unmanaged object exists in a
stack frame above the point at which a managed exception is raised and below
the frame where the managed exception is caught and the exception frame in
which that unmanaged obect was optimized away, you'll never see a problem.

So it's a pretty special case that doesn't work. If your calling pattern is
always managed -> native and never vice-versa, I don't think you'd ever see
a problem.

-cd
Nov 17 '05 #10
Steve McLellan wrote:
"Javier Estrada" <lj*******@hotmail.com> wrote in message
news:eJ**************@TK2MSFTNGP10.phx.gbl...
Thx. Very helpful information--thus the MVP title, eh?


I always thought you had to climb tall mountains and meditate with
wizened gurus to get the MVPs.. that's my illusions shattered..


The mountain-climbing does help... ;-)

-cd
Nov 17 '05 #11
Steve McLellan wrote:
"Gerhard Menzl" <ge***********@spamspamspamlovelyspam.net> wrote in
message news:41********@news.kapsch.co.at...
Ronald Laeremans [MSFT] wrote:
And if you are compiling parts of your call tree with /clr you need
to use /EHa as well since the CLR uses SEH exceptions as its
underlying mechanism.


I find this rather hard to swallow. When creating a Managed C++
project, the IDE will automatically set /clr and /EHsc. Does this
mean that you get incorrect compiler settings by default? And why do
managed exceptions seem to work in my application?


I also didn't know about this, and it seems a rather dangerous
omission. I don't know of any problems we've had NOT using /EHa,
however. There also doesn't appear to be any option in the IDE to
enable /EHa - which means that the development team either didn't
have a lot of problems not using it, or added it programmatically to
the command line but didn't add it to the IDE for whatever reason?


I believe you have to add it explicitly to the command line in the IDE.
This is fixed in Whidbey - /EHa is added authomatically, and attempting to
compile with /clr /EHs produces an error.

See my other reply (to Gerhard Menzl) for why you've probably never noticed
anything not working.

-cd
Nov 17 '05 #12
Carl Daniel [VC++ MVP] wrote:
Unless you produce a construct in which an unmanaged object exists in
a stack frame above the point at which a managed exception is raised
and below the frame where the managed exception is caught and the
exception frame in which that unmanaged obect was optimized away,
....and the destructor of the native object does something important enough
that you'd notice if it wasn't called...
you'll never see a problem.


-cd
Nov 17 '05 #13
Carl Daniel [VC++ MVP] wrote:

replace __set_se_handler with _set_se_translator throughout the posting
above.

-cd
Nov 17 '05 #14
>>>
And if you are compiling parts of your call tree with /clr you need
to use /EHa as well since the CLR uses SEH exceptions as its
underlying mechanism.

I find this rather hard to swallow. When creating a Managed C++
project, the IDE will automatically set /clr and /EHsc. Does this
mean that you get incorrect compiler settings by default? And why do
managed exceptions seem to work in my application?


I also didn't know about this, and it seems a rather dangerous
omission. I don't know of any problems we've had NOT using /EHa,
however. There also doesn't appear to be any option in the IDE to
enable /EHa - which means that the development team either didn't
have a lot of problems not using it, or added it programmatically to
the command line but didn't add it to the IDE for whatever reason?


I believe you have to add it explicitly to the command line in the IDE.
This is fixed in Whidbey - /EHa is added authomatically, and attempting to
compile with /clr /EHs produces an error.

See my other reply (to Gerhard Menzl) for why you've probably never
noticed anything not working.

Have done, and yes, it probably does explain it. Presumably none of the MS
engineers came across it either (or they're not telling). Good to know (as
per usual) that "it's fixed in the next version" :-)

Thanks,

Steve
Nov 17 '05 #15
Carl Daniel [VC++ MVP] wrote:
That's what it means, yes - MC++ projects created by VC++ 2003 don't handle
exceptions properly.

Unless you produce a construct in which an unmanaged object exists in a
stack frame above the point at which a managed exception is raised and below
the frame where the managed exception is caught and the exception frame in
which that unmanaged obect was optimized away, you'll never see a problem.

So it's a pretty special case that doesn't work. If your calling pattern is
always managed -> native and never vice-versa, I don't think you'd ever see
a problem.


Hm, I have managed objects that contain Standard Library containers of
(gcrooted) managed objects that may throw. And our release-or-perish
deadline is a mere fortnight away.

Words like "creeps" or "heebie-jeebies" come to mind ...

--
Gerhard Menzl

#dogma int main ()

Humans may reply by replacing the obviously faked part of my e-mail
address with "kapsch".
Nov 17 '05 #16

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

Similar topics

1
by: Noel | last post by:
Hi, I am a tad confused about if there are any benefits from using asynchronous vs synchronous network communication. As my example, I have been writing a dns lookup stack with a network...
1
by: Chris | last post by:
Hi. I have a ibrary I'm trying to use via javascript within IE. This library uses an asynchronous model where I call into a function and pass it a callback function as one of its arguments. My...
3
by: Pro1712 | last post by:
Hi, this may be a stupid question: How can I can call the DoWork-function of a BackgroundWorker synchronous? Or in other words: How can I extend the BackgroundWorker class with a function...
4
by: Ryan Liu | last post by:
TcpClient has a method called GetworkStream GetStream(); So in other words, there is only one stream associate with it for input and output, right? So while it is receiving, it can not send, and...
4
by: keyofdminor | last post by:
Folks, Short version: Has anyone tried a synchronous call ("SJAX") to a server with the Prototype library? I'm curious if there is a bug in the library (possible) or if I am making mistake...
3
by: =?Utf-8?B?bWs=?= | last post by:
Hi everyone, I need to refactor some of our processes using the asynchronous programming model. I have defined a couple of arrays of delegates to pipline the asynchronous invocations through...
5
by: HugeBob | last post by:
Hi All, I've got a question about Asynchronous vs Synchronous mode with the XMLHttpRequest object. What are the ramifications of using one mode vs the other? If the script uses Asynchronous...
0
ammoos
by: ammoos | last post by:
hi friends pls help me.. i got an assignment which i feel very difficult to me.. i dont have more knowledge about multi-threading in .net... the assignment details is below.... pls help me... i...
6
by: Simon | last post by:
Hi All, An experiment i'm doing requires requires a synchronous cross-domain request, without using a proxy. I wondered if anyone had any ideas to help me achieve this. Below is what I have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.