Microsoft C + + language extension: try-except statement structured exception

Source: Internet
Author: User
Tags exception handling microsoft c
Microsoft-Only





The try-except statement is a Microsoft C + + language extension that enables an application to gain control of a program when an event that normally terminates execution occurs. Such events are called exceptions, and the mechanism for handling exceptions is called structured exception handling.



Exceptions may be based on hardware or software. Even if an application cannot fully recover from a hardware or software exception, structured exception handling can help diagnose problems by displaying error messages and capturing the internal state of the application. This is especially useful for intermittent problems that cannot be easily reproduced.



Grammar



try-except-statement:
__try compound-statement



__except ( expression ) compound-statement



__tryThe compound statement after the clause is a protected section. The__exceptcompound statement after the clause is an exception handler. If an exception is thrown during the execution of the control section, the handler specifies a series of actions to be taken. The execution process is as follows:



    1. Executes the protected section.


    2. If no exception occurs during the execution of the protected section, the__exceptstatement after the clause is resumed.


    3. If an exception occurs during the execution of a protected section or in any routine called by a protected section, an expression is evaluated__exceptand the returned value determines how the exception is handled. There are three values:
    4. EXCEPTION_CONTINUE_SEARCHThe exception is not recognized. Continue searching up the stack lookup handler, first the try-except statement that is located, and then the handler with the next highest priority.


    5. EXCEPTION_CONTINUE_EXECUTIONThe exception is recognized, but is closed. Continue execution from the point at which the exception occurred.


      EXCEPTION_EXECUTE_HANDLERThe exception is recognized. The control of the__exceptexception handler is transferred by executing a compound statement, and then continues execution at the point where the exception occurred.



Because an__exceptexpression is evaluated as a C expression, it is limited to a single value, a conditional expression operator, or a comma operator. If more processing is required, the expression can call a routine that returns one of the three values listed above.


Description

Structured exception handling applies to C and C + + source files. However, this is not specifically designed for C + +. You can ensure that your code is more portable by using C + + exception handling. In addition, the C + + exception handling mechanism is much more flexible because it can handle any type of exception.

Description

For C + + programs, you should use C + + exception handling instead of structured exception handling. For more information, see Exception Handling in the C + + language Reference.


Each routine in the application can have its own exception handler. An__exceptexpression__tryexecutes within the bounds of the body. This means that it can access any local variables declared at that point.



__leaveThe keyword is valid in the try-except statement block.__leavethe effect is to jump to the end of the try-except block. Execution resumes after the exception handler finishes. Although statements can be usedgototo achieve the same result, thegotostatement causes the stack to unwind. Because__leavestatements do not involve stack unwinding, they are more efficient.



Using thelongjmprun-time function to exit the try-except statement is considered an exception termination. Jumping to a__trystatement is illegal, but jumping out of the statement is legal. If a process is canceled during the execution of the try-except statement, the exception handler is not called.



Example



The following is an example of an exception handler and a termination handler. For more information about terminating handlers, see try-finally statements.


.  
.  
.  
puts("hello");  
__try{  
   puts("in try");  
   __try{  
      puts("in try");  
      RAISE_AN_EXCEPTION();  
   }__finally{  
      puts("in finally");  
   }  
}__except( puts("in filter"), EXCEPTION_EXECUTE_HANDLER ){  
   puts("in except");  
}  
puts("world");


This is the output of the above example, and a comment is added to the right:


hello  
in try              /* fall into try                     */  
in try              /* fall into nested try                */  
in filter           /* execute filter; returns 1 so accept  */  
in finally          /* unwind nested finally                */  
in except           /* transfer control to selected handler */  
world               /* flow out of handler                  */


End Microsoft Private



Related articles:



C-language php HelloWorld extension



C Language Structure function: the difference between the language structure and the function of PHP


Related Article

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.