try-finally 语句 (C)try-finally Statement (C)

Microsoft 专用Microsoft Specific

try-finally 语句是 C 语言的 Microsoft 扩展,用于使应用程序能够在代码块的执行被中断时保证清理代码的执行。The try-finally statement is a Microsoft extension to the C language that enables applications to guarantee execution of cleanup code when execution of a block of code is interrupted. 清理包括多个任务,如释放内存、关闭文件和释放文件句柄。Cleanup consists of such tasks as deallocating memory, closing files, and releasing file handles. try-finally 语句对此类例程特别有用:具有几个位置,在这些位置上执行了检查以找出可能导致例程提前返回内容的错误。The try-finally statement is especially useful for routines that have several places where a check is made for an error that could cause premature return from the routine.

try-finally-statement:try-finally-statement:
__try compound-statement__try compound-statement

__finally compound-statement__finally compound-statement

__try 子句后的复合语句是受保护节。The compound statement after the __try clause is the guarded section. __finally 子句后的复合语句是终止处理程序。The compound statement after the __finally clause is the termination handler. 该处理程序将指定在退出受保护节时执行的一组操作,无论该受保护节是因异常(非正常终止)还是标准贯穿(正常终止)而退出。The handler specifies a set of actions that execute when the guarded section is exited, whether the guarded section is exited by an exception (abnormal termination) or by standard fall through (normal termination).

控制权通过简单的顺序执行(贯穿)传递到 __try 语句。Control reaches a __try statement by simple sequential execution (fall through). 当控制权交给 __try 语句时,其关联的处理程序将变为活动状态。When control enters the __try statement, its associated handler becomes active. 执行过程如下所示:Execution proceeds as follows:

  1. 执行受保护节。The guarded section is executed.

  2. 调用终止处理程序。The termination handler is invoked.

  3. 当终止处理程序完成时,执行在 __finally 语句后继续。When the termination handler completes, execution continues after the __finally statement. 无论受保护节如何结尾(例如,通过受保护体外部的 goto 语句或通过 return 语句),终止处理程序都在控制流移出受保护节之前执行。Regardless of how the guarded section ends (for example, via a goto statement out of the guarded body or via a return statement), the termination handler is executed before the flow of control moves out of the guarded section.

    __leave 关键字在 try-finally 语句块中有效。The __leave keyword is valid within a try-finally statement block. __leave 的效果是跳转到 try-finally 块的末尾。The effect of __leave is to jump to the end of the try-finally block. 终止处理程序将立即执行。The termination handler is immediately executed. 尽管可使用 goto 语句来达到相同的结果,但 goto 语句会导致堆栈展开。Although a goto statement can be used to accomplish the same result, a goto statement causes stack unwinding. 由于 __leave 语句不涉及堆栈展开,因此更有效。The __leave statement is more efficient because it does not involve stack unwinding.

    使用 try-finally 语句或 return 运行时函数退出 longjmp 语句被视为异常终止。Exiting a try-finally statement using a return statement or the longjmp run-time function is considered abnormal termination. 跳转到 __try 语句是非法的,但跳出该语句是合法的。It is illegal to jump into a __try statement, but legal to jump out of one. 必须运行在起点和终点之间处于活动状态的所有 __finally 语句。All __finally statements that are active between the point of departure and the destination must be run. 这称为“局部展开”。This is called a "local unwind."

    如果在执行 try-finally 语句时取消了进程,则不会调用终止处理程序。The termination handler is not called if a process is killed while executing a try-finally statement.

备注

结构化异常处理适用于 C 和 C++ 源文件。Structured exception handling works with C and C++ source files. 但是,这不是专门为 C++ 设计的。However, it is not specifically designed for C++. 您可通过使用 C++ 异常处理来确保提高代码的可移植性。You can ensure that your code is more portable by using C++ exception handling. 此外,C++ 异常处理机制灵活得多,因为它可以处理任何类型的异常。Also, the C++ exception handling mechanism is much more flexible, in that it can handle exceptions of any type.

备注

对于 C++ 程序,应使用 C++ 异常处理,而不是结构化异常处理。For C++ programs, C++ exception handling should be used instead of structured exception handling. 有关详细信息,请参阅《C++ 语言参考》中的异常处理For more information, see Exception Handling in the C++ Language Reference.

请参阅 try-except statement 的示例以了解 try-finally 语句如何运行。See the example for the try-except statement to see how the try-finally statement works.

结束 Microsoft 专用END Microsoft Specific

另请参阅See Also

try-finally 语句try-finally Statement