确保在 Try 语句完成之后总是执行一条或多条语句.
Finally Statement
Finally
{
语句
}
每次使用 finally 都必须附属于(与之关联) 它上面的 try 语句(在任何可选的 catch 和/或 else 之后). finally 总是附属于它上面且离它最近的无主 try 语句, 不过可以使用区块改变这种行为.
Try 语句的行为取决于 catch 或 finally 是否存在. 有关详情, 请参阅 Try.
不能使用 Goto, break, continue 和 return 退出 finally 区块, 因为这需要在 try 区块里禁止任何控制流指令. 例如, 如果 try 语句中使用 return 42, 值 42 将会在 finally 区块执行后被返回. 如果使用上述语句之一来试图跳出 finally 区块, 将会在加载时或运行时被检测为错误.
如果脚本被任何方式直接终止, 包括托盘菜单, ExitApp, Finally 语句不会被执行.
可以在 finally 语句中使用 One True Brace(OTB) 样式. 例如:
try {
...
} finally {
...
}
try {
...
} catch {
...
} else {
...
} finally {
...
}
try
{
ToolTip "Working..."
Example1()
}
catch as e
{
; 关于 e 对象的更多细节, 请参阅 Error.
MsgBox(Type(e) " thrown!`n`nwhat: " e.what "`nfile: " e.file
. "`nline: " e.line "`nmessage: " e.message "`nextra: " e.extra,, 16)
}
finally
{
ToolTip ; 隐藏工具提示
}
MsgBox "Done!"
; 此函数包含了清理代码的 Finally 区块
Example1()
{
try
Example2()
finally
MsgBox "This is always executed regardless of exceptions"
}
; 当系统分钟数为奇数时此函数执行会报错
Example2()
{
if Mod(A_Min, 2)
throw Error("That's odd...")
MsgBox "Example2 did not fail"
}