Difference between a C++ exception and Structured Exception

Krishnan picture Krishnan · Sep 24, 2010 · Viewed 10.5k times · Source

Can someone explain the difference between a C++ exception and a structured exception in MFC?

Answer

peterchen picture peterchen · Sep 24, 2010

You actually have three mechanisms:

  • C++ exceptions, implemented by the compiler (try/catch)
  • Structured Exception Handling (SEH), provided by Windows (__try / __except)
  • MFC exception macros (TRY, CATCH - built on top of SEH / C++ exceptions - see also TheUndeadFish's comment)

C++ exceptions usually guarantee automatic cleanup during stack unwinding (i.e. destructors of local objects run), the other mechanisms don't.

C++ exceptions only occur when they are explicitly thrown. Structured Exceptions may occur for many operations, e.g. due to undefined behavior, passing invalid pointers to APIs, unmounting the backing store of a memory mapped file, and many more.

MFC did introduce the exception macros to support exceptions even if compilers didn't implement them.