Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between a C++ exception and Structured Exception

Tags:

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

like image 837
RK- Avatar asked Sep 24 '10 11:09

RK-


People also ask

What is structured 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 C++ support SEH, we recommend that you use ISO-standard C++ exception handling. It makes your code more portable and flexible.

What is structured exception handling C#?

C# provides a structured solution to the exception handling in the form of try and catch blocks. Using these blocks the core program statements are separated from the error-handling statements. These error handling blocks are implemented using the try, catch, and finally keywords.

What are the different types of exceptions in CPP?

There are two types of exceptions: a)Synchronous, b)Asynchronous (i.e., exceptions which are beyond the program's control, such as disc failure, keyboard interrupts etc.). C++ provides the following specialized keywords for this purpose: try: Represents a block of code that can throw an exception.

What is an exception C?

C # in Telugu An exception is a problem that arises during the execution of a program. A C++ exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero. Exceptions provide a way to transfer control from one part of a program to another.


2 Answers

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.

like image 182
peterchen Avatar answered Sep 23 '22 00:09

peterchen


It is a heavy MSVC++ implementation detail, but on Windows a C++ exception is also a SEH exception. The exception code is 0xE04D5343 (last three bytes = 'MSC'). And all of the regular SEH support is used to unwind the stack, get automatic cleanup code to run and to filter the exception so the proper catch clause is selected. As demonstrated here.

Getting the thrown exception object in the filter expression is plumbing that's added by the CRT beyond what SEH provides, necessarily so since it is C++ specific.

A further implementation detail is the /EH compiler setting. The default (/EHsc) allows the compiler to optimize the code that's generated and suppress the exception filters that are needed to run automatic cleanup. If it can see that none of the emitted C++ code can throw an exception. To get automatic cleanup for SEH exceptions you have to compile with /EHa so that this optimization is suppressed.

One strategy to marry C++ exceptions with SEH is to use _set_se_translator() so you can translate an SEH exception to a C++ exception. Albeit that it isn't often wise to catch SEH exceptions, they are almost always nasty. You'd normally favor using __try/__catch, as shown in the linked answer.

like image 28
Hans Passant Avatar answered Sep 21 '22 00:09

Hans Passant