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
> Well, it seems like it's bad both ways, isn't it.
>
[quoted text clipped - 4 lines]
> 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
Javier Estrada - 17 Dec 2004 21:54 GMT
Thx. Very helpful information--thus the MVP title, eh?
Steve McLellan - 20 Dec 2004 13:10 GMT
> 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
Carl Daniel [VC++ MVP] - 20 Dec 2004 15:01 GMT
>> 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
Carl Daniel [VC++ MVP] - 20 Dec 2004 15:12 GMT
replace __set_se_handler with _set_se_translator throughout the posting
above.
-cd