确保在try语句后总是会执行的一个或多个语句(命令或表达式)。
Finally Statement
Finally
{
Statements
}
每次使用finally都必须附属于(与之关联)它上面的try(或catch)语句。finally总是附属于它上面且离它最近的无主try语句,不过可以使用区块改变这种行为。
如果finally语句使用在不含catch块的try语句中,则以后无法清除这里出现的异常且在finally语句执行后异常传播仍会继续。
不允许使用goto/break/continue退出finally语句,因为这样会中断异常传播(不过可以在区块中正常使用以确保持续执行)。在加载时会检测到这种错误(以及运行时的动态goto语句)。
One True Brace (OTB)风格可以用于finally命令中。例如:
try {
...
} finally {
...
}
try {
...
} catch e {
...
} finally {
...
}
try
{
ToolTip, Working...
Example1()
}
catch e
{
; 关于e对象的更多细节,请参阅Catch。
MsgBox, 16,, % "Exception thrown!`n`nwhat: " e.what "`nfile: " e.file
. "`nline: " e.line "`nmessage: " e.message "`nextra: " e.extra
}
finally
{
ToolTip ; 隐藏工具提示
}
MsgBox, Done!; 此函数包含了清理代码的Finally区块
Example1()
{
try
Example2()
finally
MsgBox, This is always executed regardless of exceptions
}
; 分钟数为奇数时此函数执行时出错
Example2()
{
if Mod(A_Min, 2)
throw Exception("Test exception")
MsgBox, Example2 did not fail
}