Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of try-except statement in Microsoft C++ language extension

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you what the use of try-except sentences in the Microsoft C++ language expansion, I believe most people do not understand, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

Dedicated to Microsoft

The try-except statement is an extension of the Microsoft C++ language that enables an application to gain control of a program when an event occurs that normally terminates execution. 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 the application cannot fully recover from hardware or software exceptions, structured exception handling can help diagnose the problem 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

The compound statement after the _ _ try clause is a protected section. The compound statement after the _ _ except 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:

Execute the protected section.

If no exception occurs during the execution of the protected section, the statement after the _ _ except clause continues.

If an exception occurs during the execution of a protected section or in any routine called by a protected section, the _ _ except expression is evaluated and the returned value determines how the exception is handled. There are three values:

The EXCEPTION_CONTINUE_SEARCH exception is not recognized. Continue to search the stack for handlers, first the try-except statement you are in, and then the handler with the next highest priority.

The EXCEPTION_CONTINUE_EXECUTION exception is recognized but closed. Continue execution from the point where the exception occurred.

The EXCEPTION_EXECUTE_HANDLER exception is recognized. Transfer control of the exception handler by executing the _ _ except compound statement, and then continue execution where the exception occurs.

Because the _ _ except expression will be evaluated as a C expression, it is limited to a single value, conditional expression operator, or 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 better code portability 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, C++ exception handling should be used instead of structured exception handling. For more information, see exception handling in the C++ language reference.

Each routine in an application can have its own exception handler. The _ _ except expression executes within the scope of the _ _ try body. This means that it can access any local variable declared there.

The _ _ leave keyword is valid in a try-except statement block. The effect of _ _ leave is to jump to the end of the try-except block. Execution will resume after the exception handler finishes. Although the goto statement can be used to achieve the same result, the goto statement causes the stack to expand. The _ _ leave statement is more efficient because it does not involve stack unfolding.

Exiting a try-except statement using the longjmp runtime function is considered an abnormal termination. It is illegal to jump to the _ _ try statement, but it is legal to jump out of it. If a process cancels during the execution of a try-except statement, the exception handler is not called.

Example

The following are examples of exception handlers and termination handlers. For more information about termination 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, with comments 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 * / above is all the content of this article entitled "what is the use of try-except sentences in Microsoft C++ language extensions". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report