大网数据广告活动
性能与资源

Termination-Handler Syntax

· 52CE 我爱测

The __try and __finally keywords are used to construct a termination handler. The following example shows the structure of a termination handler.

The __try and __finally keywords are used to construct a termination handler. The following example shows the structure of a termination handler.

__try 
{ 
    // guarded body of code 
 
} 
__finally 
{ 
    // __finally block 
 
}

For examples, see Using a Termination Handler.

As with the exception handler, both the __try block and the __finally block require braces ({}), and using a goto statement to jump into either block is not permitted.

The __try block contains the guarded body of code that is protected by the termination handler. A function can have any number of termination handlers, and these termination-handling blocks can be nested within the same function or in different functions.

The __finally block is executed whenever the flow of control leaves the __try block. However, the __finally block is not executed if you call any of the following functions within the __try block: ExitProcess, ExitThread, or abort.

The __finally block is executed in the context of the function in which the termination handler is located. This means that the __finally block can access that function's local variables. Execution of the __finally block can terminate by any of the following means.

  • Execution of the last statement in the block and continuation to the next instruction
  • Use of a control statement (return, break, continue, or goto)
  • Use of longjmp or a jump to an exception handler

If execution of the __try block terminates because of an exception that invokes the exception-handling block of a frame-based exception handler, the __finally block is executed before the exception-handling block is executed. Similarly, a call to the longjmp C run-time library function from the __try block causes execution of the __finally block before execution resumes at the target of the longjmp operation. If __try block execution terminates due to a control statement (return, break, continue, or goto), the __finally block is executed.

The AbnormalTermination function can be used within the __finally block to determine whether the __try block terminated sequentially — that is, whether it reached the closing brace (}). Leaving the __try block because of a call to longjmp, a jump to an exception handler, or a return, break, continue, or goto statement, is considered an abnormal termination. Note that failure to terminate sequentially causes the system to search through all stack frames in reverse order to determine whether any termination handlers must be called. This can result in performance degradation due to the execution of hundreds of instructions.

To avoid abnormal termination of the termination handler, execution should continue to the end of the block. You can also execute the __leave statement. The __leave statement allows for immediate termination of the __try block without causing abnormal termination and its performance penalty. Check your compiler documentation to determine if the __leave statement is supported.

If execution of the __finally block terminates because of the return control statement, it is equivalent to a goto to the closing brace in the enclosing function. Therefore, the enclosing function will return.

Source: Microsoft Learn / Windows Win32 documentation by Microsoft and contributors. Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.

资料来源与适用范围

资料来源
Microsoft Learn / Windows Win32 documentation
适用范围
Windows Win32 API · Source date 07/14/2025
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。