Abnormal processing method
1. Windows SEH and C Exception
1) Windows SEH structured abnormalities
Structured exception is the unrelated exception handling mechanism provided by the Windows operating system, and SHE uses the RaiseException () function in Win32API to throw an exception. Use the keyword __TRY and keyword __except to capture, and use macro functions in the VC GetExceptionCode and getExceptionInfo come to get the cause of the captured exception, and generate an anomaly when the anomalin is generated. __finalLY keyword assurance whether the Finally code segment will be executed regardless of whether an exception occurs.
SHE uses sample code
Int ecode;
__TRY
{
__TRY
{
RaiseException (1, // Throw an exception 1 of 1
0,
0, null); // No parameters
}
__finally
{
Printf ("2"); // Whether there is an abnormality, the code will be executed
}
}
__except (ecode = getExceptioncode ())
{
Printf ("Exceptions, CODE =% D / N", Ecode); // Capture the code after exception;
}
Output results:
2 exceptions, code = 1
2) C Exception
The C standard also provides an exception handling mechanism that expresses the use of try, catch, throw keywords, can throw simple variables, complex variables and abnormal objects through the throw function, compared to Windows exceptions, exception objects You can provide more information to developers.
Try
{
// Normal code
. . .
Throw Cexcetion ();
. . .
}
Catch (CEXCEPTION * E)
{
// Treat an exception code
}
3) SEH to C abnormal conversion
In the same program, if you use Win32API, it will throw SHE, use the C library function, which will throw the C exception, Win32API and C functions, if you use two exception capture mechanisms, use it, use it will affect the program. Readability, therefore the C runtime provides the _SET_SE_TRANSLATOR function, and the SEH exception is converted by a callback mode when the She is abnormal. A conversion macro is provided here to achieve conversion.
Convert macro code:
#define install_sehconvert () ExceptionConvert EcexceptionConvert
Class Sehexception
{
Private:
UNSIGNED INT NSE;
PUBLIC:
Sehexception () {}
Sehexception (unsigned int N): NSE (n) {}
~ Sehexception () {}
Unsigned int getsenumber () {return nse;}
}
Class ExceptionConvert
{
PUBLIC:
ExceptionConvert () {oldfanc = _SET_SE_TRANSLATOR (TRANS_FUNC);}
~ ExceptionConvert () {_ set_se_translator (oldfanc);
Private:
Static void trans_func (unsigned int u, exception_pointers * pEXP) {
Throw sehexception (u);
}
_SE_TRANSLATOR_Function Oldfanc;
}
Use the following INSTALL_SEHCONVERT macro to use the following code to capture the Shenceness
Install_sehconvert ();
Try
{
...
}
Catch (sehexception & seh) {
...
}
2. Synchronous abnormalities and asynchronous exceptions
1) The C Exception of VC uses two modes to capture exceptions: synchronous mode and asynchronous mode. The project's debugging version of the VC default uses asynchronous mode, and the project's release is default synchronization mode. In synchronous mode, the compiler of the VC assumes that only the code is only in the display using the throw and the call function, so in synchronous mode, the code compiled by the VC is smaller, but in this mode, TRY -catch pair to unusually captured memory access abnormalities and arithmetic division from zeroing. In asynchronous mode, the VC compiler generates an exception capture code for each statement in the TRY block. In this case, he can capture all exceptions and ensure that the object on the stack is properly released in the stack. In order to capture all exceptions in the release version, you need to turn on the asynchronous mode, but the price is the program compiling code, and the running speed is slow.
2) Compile option:
Compile options in sync mode are / eHS or / gx (equivalent to / eHSC)
Compilation option for asynchronous mode is / eHA
3. Abnormal capture under multi-thread
In a function of creating a thread and running a thread, placing the code in the TRY in the TRY block does not capture an exception that occurs in a thread function, and the exception that occurs in the thread function can only be captured in the thread function. And each thread requires its own SHE conversion macro. Conversion macro can be placed in the beginning of thread function
4. Reference MSDN library