Can someone explain the difference between a C++ exception and a structured exception in MFC?
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.
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.
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.
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.
You actually have three mechanisms:
try
/catch
)__try
/ __except
)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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With