Http://bbs.ustc.edu.cn/cgi-bin/bbsgcon? BN = cplusplus & FN = g431ef019 & num = 224
By default, VC captures synchronization exceptions, so only throw is a formal exception.
If memory access error or division by zero occurs, the stack cannot be correctly resolved (the objects on the stack may be
Not destructed)
1. Windows seh and C ++ exception
1) Windows seh structured exception
Structured exception is a language-independent exception handling mechanism provided by the Windows operating system.
The raiseexception () function in WIN32API throws an exception and uses the keyword _ Try and key in VC.
Use the macro functions getexceptioncode and getexceptioninfo to capture
The cause of the exception and the environment status when the exception occurs. The _ finally keyword ensures that
Exception. All finally code segments are executed.
She sample code
Int ecode;
_ Try
{
_ Try
{
Raiseexception (1, // throw a seh exception with An Exception Code of 1
0,
0, null); // No Parameter
}
_ Finally
{
Printf (2); // the code that will be executed no matter whether there is an exception or not
}
}
_ Response T (ecode = getexceptioncode ())
{
Printf (exception, code = % d \ n, ecode); // capture the code executed after the exception;
}
Output result:
2 exception, code = 1
2) C ++ exception
The C ++ standard also provides an exception handling mechanism, which is expressed by using the try, catch, and throw keywords.
C ++ exceptions can be thrown by using the throw function. Complex Variables and exception objects are different from those of windows.
Exception objects can provide more information to developers.
Try
{
// Normal code
...
Throw cexcetion ();
...
}
Catch (cexception * E)
{
// Handle the Exception Code
}
3) abnormal conversions from seh to C ++
In the same program, if WIN32API is used, it will throw she and use the C ++ library functions, and they will throw
C ++ exception. If two exception capture mechanisms are used in combination with WIN32API and C ++ function
Therefore, the C ++ Runtime Library provides the _ set_se_translator function.
The seh exception is converted to a C ++ exception in the callback mode. Here we provide a conversion macro to implement Conversion
Change.
Code for macro conversion:
# Define install_sehconvert () exceptionconvert ecexceptionconvert
Class sehexception
{
PRIVATE:
Unsigned int neuron;
Public:
Sehexception (){}
Sehexception (unsigned int N): neuron (n ){}
~ Sehexception (){}
Unsigned int getsenumber () {return neuron ;}
};
Class exceptionconvert
{
Public:
Predictionconvert () {oldfanc = _ set_se_translator (trans_func );}
~ Predictionconvert () {_ set_se_translator (oldfanc );}
PRIVATE:
Static void trans_func (unsigned int U,
Prediction_pointers * pexp)
{
Throw sehexception (U );
}
_ Se_translator_function oldfanc;
};
After using the above install_sehconvert macro, you can use the following code to capture the she exception.
Install_sehconvert ();
Try
{
...
}
Catch (sehexception & seh ){
...
}
2. Synchronization exceptions and asynchronous exceptions
1) vc c ++ exception uses two modes to capture exceptions: Synchronous mode and asynchronous mode. VC Project
The debug version uses asynchronous mode by default, and the release version of the project uses synchronous mode by default. In synchronous mode
The compiler assumes that an exception is thrown only when throw and function call are displayed in the Code. Therefore
In step mode, the Code Compiled by VC is relatively small, but in this mode, try-Catch cannot capture memory access.
Exception and zero division in arithmetic operations. In asynchronous mode, the VC compiler generates an exception for each statement in the try block.
Capture code. In this case, it can capture all exceptions and ensure that the objects on the stack are correctly interpreted in the stack.
. In order to capture all exceptions in the release version, you need to enable the asynchronous mode, but the cost is that the program Compilation
The output Code increases and the running speed slows down.
2) Compilation options:
The compilation option for the synchronization mode is/EHS or/GX (equivalent to/ehsc)
The asynchronous mode compilation option is/EHA.
3. multi-threaded exception capture
Placing the code of the created thread in the try block in the function that creates and runs the thread does not capture the code in the thread function.
The exceptions that occur in the thread function can only be caught in the thread function. And every thread needs
Convert your own she macro. The conversion macro can be placed at the beginning of the thread function.
4. Refer to the msdn Library