Win32 structured exceptions vs C++ exceptions 
Author Message
 Win32 structured exceptions vs C++ exceptions

I'm trying to find out about the interaction between Win32 C exceptions
and C++ exceptions in VC++ 6 sp2.

Win32 programs generate exceptions on runtime errors like null pointer
dereferences, etc. These are Win32 exceptions, not C++ exceptions, but
sometimes they can be caught with catch(...). Whether they can be
caught or not seems to depend in some undocumented way upon the
optimisations used to compile the program. Specifically, build this toy
program with a default workspace:

   void main() {
      try {
         char* bad = 0; // I wouldn't normally do this kind of thing
         *bad = 0; // Generates EXCEPTION_ACCESS_VIOLATION
      }
      catch(...) {
         // This should catch the exception
      }
   }

The default "Win32 Release" configuration crashes with an application
error dialog box. But if I turn off global optimization with "/Og-",
the catch(...) catches the exception and all is sweetness and light.

Does anyone have more details of this? I'm ploughing on anyway, but I
don't want to reinvent the wheel. I'm adapting some Microsoft code that
uses _set_se_translator() to turn Win32 exceptions into C++ exceptions.

Thanks,
Bennett.

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Sun, 28 Apr 2002 03:00:00 GMT  
 Win32 structured exceptions vs C++ exceptions
Bennett,

You might be interested in looking up Bobby Schmidt's column on MSDN online,
as he has covered exceptions pretty well...

--
Tomas Restrepo

http://members.xoom.com/trestrep/


Quote:
> I'm trying to find out about the interaction between Win32 C exceptions
> and C++ exceptions in VC++ 6 sp2.

> Win32 programs generate exceptions on runtime errors like null pointer
> dereferences, etc. These are Win32 exceptions, not C++ exceptions, but
> sometimes they can be caught with catch(...). Whether they can be
> caught or not seems to depend in some undocumented way upon the
> optimisations used to compile the program. Specifically, build this toy
> program with a default workspace:

>    void main() {
>       try {
>          char* bad = 0; // I wouldn't normally do this kind of thing
>          *bad = 0; // Generates EXCEPTION_ACCESS_VIOLATION
>       }
>       catch(...) {
>          // This should catch the exception
>       }
>    }

> The default "Win32 Release" configuration crashes with an application
> error dialog box. But if I turn off global optimization with "/Og-",
> the catch(...) catches the exception and all is sweetness and light.

> Does anyone have more details of this? I'm ploughing on anyway, but I
> don't want to reinvent the wheel. I'm adapting some Microsoft code that
> uses _set_se_translator() to turn Win32 exceptions into C++ exceptions.

> Thanks,
> Bennett.

> Sent via Deja.com http://www.deja.com/
> Before you buy.



Sun, 28 Apr 2002 03:00:00 GMT  
 Win32 structured exceptions vs C++ exceptions


Quote:
> You might be interested in looking up Bobby Schmidt's column on MSDN
> online, as he has covered exceptions pretty well...

Thanks for replying. Yes, I've been eagerly reading each superb
instalment. However, he's really only covered standard C++ exceptions
in a fairly platform-neutral way. My problem is specific to Win32.

Cheerio,
Bennett.

Sent via Deja.com http://www.deja.com/
Before you buy.



Mon, 29 Apr 2002 03:00:00 GMT  
 Win32 structured exceptions vs C++ exceptions

Quote:

> I'm trying to find out about the interaction between Win32 C
> exceptions and C++ exceptions in VC++ 6 sp2.

Due to the furore that has failed to appear over this question, I'll
answer it myself. (See below for the full story.)

Since the default VC++ configuration uses the synchronous (C++ style)
exception mechanism, the compiler notices that there are no C++
exceptions being thrown, so it just optimises the catch() away. This is
why it starts working if you disable global optimisation.

In a normal program, even if the exception is caught there may be
problems. This is because the compiler may still have optimised out
some destructor calls that should be made as the stack unwinds from the
point of the exception to the catch. The only way to make this reliable
is to enable asynchronous exception handling with /EHa . This prevents
the optimiser removing any catch(...) clauses.

I think it's bad form for catch(...) to catch Win32 exceptions. That's
not what catch(...) is for! I guess this is another of the reviled
Microsoft extensions. Boy, if they keep this kind of behaviour up,
they're going to get into trouble with the government or something.

Cheerio,
Bennett.

Quote:
> Win32 programs generate exceptions on runtime errors like null pointer
> dereferences, etc. These are Win32 exceptions, not C++ exceptions, but
> sometimes they can be caught with catch(...). Whether they can be
> caught or not seems to depend in some undocumented way upon the
> optimisations used to compile the program. Specifically, build this
> toy program with a default workspace:

>    void main() {
>       try {
>          char* bad = 0; // I wouldn't normally do this kind of thing
>          *bad = 0; // Generates EXCEPTION_ACCESS_VIOLATION
>       }
>       catch(...) {
>          // This should catch the exception
>       }
>    }

> The default "Win32 Release" configuration crashes with an application
> error dialog box. But if I turn off global optimization with "/Og-",
> the catch(...) catches the exception and all is sweetness and light.

> Does anyone have more details of this? I'm ploughing on anyway, but I
> don't want to reinvent the wheel. I'm adapting some Microsoft code
> that uses _set_se_translator() to turn Win32 exceptions into C++
> exceptions.

> Thanks,
> Bennett.

Sent via Deja.com http://www.deja.com/
Before you buy.


Fri, 03 May 2002 03:00:00 GMT  
 Win32 structured exceptions vs C++ exceptions
Quote:

>...I guess this is another of the reviled
> Microsoft extensions. Boy, if they keep this kind of behaviour up,
> they're going to get into trouble with the government or something.

Naw, naw, that can't happen. You're just an MS-hater.

Quote:

> Cheerio,
> Bennett.

> > Win32 programs generate exceptions on runtime errors like null pointer
> > dereferences, etc. These are Win32 exceptions, not C++ exceptions, but
> > sometimes they can be caught with catch(...). Whether they can be
> > caught or not seems to depend in some undocumented way upon the
> > optimisations used to compile the program. Specifically, build this
> > toy program with a default workspace:

> >    void main() {
> >       try {
> >          char* bad = 0; // I wouldn't normally do this kind of thing
> >          *bad = 0; // Generates EXCEPTION_ACCESS_VIOLATION
> >       }
> >       catch(...) {
> >          // This should catch the exception
> >       }
> >    }

> > The default "Win32 Release" configuration crashes with an application
> > error dialog box. But if I turn off global optimization with "/Og-",
> > the catch(...) catches the exception and all is sweetness and light.

> > Does anyone have more details of this? I'm ploughing on anyway, but I
> > don't want to reinvent the wheel. I'm adapting some Microsoft code
> > that uses _set_se_translator() to turn Win32 exceptions into C++
> > exceptions.

> > Thanks,
> > Bennett.

> Sent via Deja.com http://www.deja.com/
> Before you buy.

--
len
if you must email, reply to:



Fri, 03 May 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Exceptions : Combining C++ and Structured Exception Handling ?

2. Crash In Exception Filter in Structured Exception HAndling

3. Floating Point Exceptions: MFC or Structured Exception Handling?

4. Win32 Structured Exception Handling and Object unwinding

5. Exceptions in IDE vs Exceptions from CommandPrompt

6. Translate a SEH exception into a C++ exception

7. Catching hardware exceptions using C++ exceptions

8. Catching SEC Exceptions as C++ Exceptions

9. retrieve exception context in C++ exception handler

10. catching Win32 API exceptions in C++

11. Catching Win32 exceptions with a C++ catch-block

12. Howto catch Win32 exceptions in C++?

 

 
Powered by phpBB® Forum Software