Structured Exception Handling in Visual C++

  • The /EHa compiler option supports handling of both standard C++ exceptions (software exceptions) and hardware exceptions by using the C++ try and catch clause. To implement SEH without specifying /EHa, you may use the __try, __except, and __finally syntax.
  • Specifying /EHa and trying to handle all exceptions by using catch(…) can be dangerous. In most cases, asynchronous exceptions are unrecoverable and should be considered fatal. Catching them and proceeding can cause process corruption and lead to bugs that are hard to find and fix.
  • When you use /EHsc or /EHs, the compiler assumes that exceptions can only occur at a throw statement or at a function call. This assumption allows the compiler to eliminate code for tracking the lifetime of many unwindable objects, which can significantly reduce code size. If you use /EHa, your executable image may be larger and slower, because the compiler doesn’t optimize try blocks as aggressively. It also leaves in exception filters that automatically clean up local objects, even if the compiler doesn’t see any code that can throw a C++ exception.
  • Structured Exception Handling (SEH) is a Microsoft extension to C to handle certain exceptional code situations, such as hardware faults, gracefully. Although Windows and Microsoft Visual C++ support SEH, we strongly recommend that you use ISO-standard C++ exception handling (with /EHsc or /EHs). It makes your code more portable and flexible.

C++ Programmer at Microsoft

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Scaling Kubernetes Clusters

DEVOps Part 1 # gitlab + jenkins + openshift

Turn Everyday Notebook into Blog

What to avoid when implementing ISO 27001

I’m doing TDD.

TinyML: TVM taming the final (ML) frontier

Day -5 MLOPs

Overview and Basics of Markdown

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Amit Kumar Parida

Amit Kumar Parida

C++ Programmer at Microsoft

More from Medium

Linux: Software libraries in C

Memory in C Programming

Comparing strings in C++

Dynamic Library In C