Exception Handling Method

Source: Internet
Author: User

1.


Windows seh and C ++ exception

1)


Windows seh structured exception

The structure exception is windows.
The language-independent exception handling mechanism provided by the operating system,
She uses the raiseexception () function in WIN32API to throw an exception, uses the keyword _ Try and keyword _ retry t in VC to capture the exception, and uses a macro
The getexceptioncode and getexceptioninfo functions are used to obtain the cause of the caught exception and the environment status when an exception occurs. _ Finally off
Make sure that the finally code segment is executed no matter whether an exception occurs or not.

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
} 
  }
__except (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, expressed by using the try, catch, and throw keywords. In a C ++ exception, you can throw a simple variable through the throw function, complex Variables and exception objects can provide developers with more information than Windows exceptions.

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 C ++ library functions, and they will throw a C ++ exception, when the WIN32API and C ++ functions are used in combination, if two exception capture mechanisms are used, the readability of the program will be affected. Therefore, the C ++ Runtime Library provides


_ Set_se_translator
Function. When a She exception occurs, the seh exception is converted to a C ++ exception through callback. A Conversion macro is provided here to implement conversion.

Code for macro conversion:

# Define
Install_sehconvert () exceptionconvert ecexceptionconvert

Class
Sehexception

{

Private
:




Unsigned

Int
AUC;

Public
:



Sehexception (){}


Sehexception (
Unsigned

Int
N): AUC (n ){}



~ Sehexception (){}



Unsigned

Int
Getsenumber (){
Return
Neuron ;}

};

Class
Predictionconvert

{

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;

};

Use the above

Install_sehconvert
Macro, you can use the following code to capture the she exception.

Install_sehconvert ();

Try

{


...

}

Catch (sehexception & seh ){


...

}

2.


Synchronization exception and asynchronous exception

1)
C ++ exception of VC
Two modes are used to capture exceptions: Synchronous mode and asynchronous mode. The debug version of the VC project uses asynchronous mode by default, and the released version of the project uses synchronous mode by default. In synchronous mode, the VC compiler assumes that
The code will only cause exceptions when throw and function call are displayed. Therefore, in synchronous mode, the Code Compiled by VC is relatively small, but in this mode, try-catch pair cannot be caught
Memory Access exceptions and zero arithmetic division exceptions. In asynchronous mode, the VC compiler generates exception capture code for each statement in the try block. In this case, the VC compiler can capture all exceptions and ensure
Objects on the stack are correctly released in the stack solution. In order to capture all exceptions in the release version, you need to enable the asynchronous mode, but the cost is that the Code Compiled by the Program 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.


Exception capture in multiple threads

Placing the code of the created thread in the try block in the function that creates and runs the thread does not capture exceptions in the thread function, exceptions in a thread function can only be caught in a thread function. In addition, each thread needs its own she conversion macro. The conversion macro can be placed at the beginning of the thread function.

4. Refer to the msdn Library

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.