A summary of C++ exceptions and SEH exceptions

[How to distinguish abnormal]

  "C++ exception" is try{}catch(...){}
  "SEH exception" is __try{} __except(-1/0/1){}

  At present, all Microsoft VC compilers (from VC6 to VC2010), by default, open the compilation support for C++ exceptions (located in the project options "Code Generation" -> Enable C++ Exceptions: /EHsc, VC6 is Enable Exception handling)

[Failed to capture exception]

  For example: try{ int *a=0; *a=100; }catch(...){printf("excption!");}, when the execution reaches *a=100, it will jump to the catch block and execute printf.
  However, if the DLL or other non-EXE project created by VC6 is in Release mode, and automatic code optimization is enabled when compiling, then the result becomes that only "C++ exceptions" are enabled, that is, "C++ exceptions cannot catch SEH exceptions" . One consequence of this is that in the DLL, even like this, try{ int *a=0; *a=100; }catch(...){printf("excption!");} to protect the code, when the code is executed When the *a=100 illegal address assignment statement is reached, catch(...) cannot catch anything, which will directly cause the program to crash.
  Unless you explicitly use the "SEH exception" __try...__except(1).., this kind of Windows exception can be caught.

[Corresponding solution]

  For VC++ starting from VC7 and later, there is a third option at "Code Generation" -> "Enable C++ Exceptions": "Valid, but SEH exception (/EHa)", so that the program can be "used" C++ exceptions can also catch SEH exceptions". So whether in EXE or DLL, try{}catch(...){} can also catch Windows exceptions.
  For VC6 non-EXE projects, there are the following two methods to use try..catch.. to catch the SEH exception of Windows:
1. By turning off the optimization during compilation (disable or only use the Default level, personally feel that it is because of the compilation of VC6 The device is not very perfect).
2. Manually add the /EHa parameter in the compilation options of the current project. There is no interface to set this compilation option, it can only be added manually.
  You can choose either of these two methods. The second method is recommended. Also note that "C++ Exception" and "SEH Exception" cannot be mixed in the same function at the same time.

SEH mainly includes:

  Per-Thread type SEH (also known as **Thread Exception Handling**) is used to monitor whether a thread code is abnormal.

   Final type SEH (also known as process exception handling, filter or top-level exception handling) is used to monitor whether all threads of the entire process are abnormal. In the entire process, there is only one exception handling of this type, which can be set through SetUnhandledExceptionFilter.

[How to distinguish abnormal]

  "C++ exception" is try{}catch(...){}
  "SEH exception" is __try{} __except(-1/0/1){}

  At present, all Microsoft VC compilers (from VC6 to VC2010), by default, open the compilation support for C++ exceptions (located in the project options "Code Generation" -> Enable C++ Exceptions: /EHsc, VC6 is Enable Exception handling)

Guess you like

Origin blog.csdn.net/lr_shadow/article/details/108814223