Asynchronous Exception Handling

In Windows, you can catch an asynchronous exception (such as an access violation exception) with catch(…) clause.

struct Foo {
  ~Foo(){
    // destructor
  }
};

try{
   Foo foo;
   *reinterpret_cast<char*>(0) = 0;
}catch(…){
    // handle access violation.
}

However, the behavior differs depends on the compiler version and command line options.

[VC6.0, /EHs] An access violation is caught. A stack object is not cleaned up.

[VC6.0, /EHa] An access violation is caught. A stack object is cleaned up.

[VC7.1, /EHs] An access violation is caught. A stack object is not cleaned up.

[VC7.1, /EHa] An access violation is caught. A stack object is cleaned up.

[VC8.0, /EHs] An access violation is not caught (second chance exception happens). A stack object is not cleaned up.

[VC8.0, /EHa] An access violation is caught. A stack object is cleaned up.

So, you would get unhandled exception bugs when a compiler is upgraded from 7.1 to 8.0. It’s, however, your fault. If you want to use catch(…) for asynchronous exception handling, you must use /EHa options in the first place.

Advertisements

About Moto

Engineer who likes coding
This entry was posted in C++. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s