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 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 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:
执行受保护节。The guarded section is executed.
调用终止处理程序。The termination handler is invoked.
__finally语句后继续。When the termination handler completes, execution continues after the
return语句），终止处理程序都在控制流移出受保护节之前执行。Regardless of how the guarded section ends (for example, via a
gotostatement out of the guarded body or via a
returnstatement), the termination handler is executed before the flow of control moves out of the guarded section.
__leavekeyword is valid within a
try-finally块的末尾。The effect of
__leaveis to jump to the end of the
try-finallyblock. 终止处理程序将立即执行。The termination handler is immediately executed. 尽管可使用
gotostatement can be used to accomplish the same result, a
gotostatement causes stack unwinding. 由于
__leavestatement is more efficient because it does not involve stack unwinding.
try-finallystatement using a
returnstatement or the
longjmprun-time function is considered abnormal termination. 跳转到
__try语句是非法的，但跳出该语句是合法的。It is illegal to jump into a
__trystatement, but legal to jump out of one. 必须运行在起点和终点之间处于活动状态的所有
__finallystatements 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
结构化异常处理适用于 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.
结束 Microsoft 专用END Microsoft Specific