Finally

Ensures that one or more statements are always executed after a Try statement finishes.

Finally Statement
Finally
{
    Statements
}

備考

Every use of Finally must belong to (be associated with) a Try statement above it (after any optional Catch and/or Else). A Finally always belongs to the nearest unclaimed Try statement above it unless a block is used to change that behavior.

Try statements behave differently depending on whether Catch or Finally is present. 詳しくは、Tryをご覧ください。

Tryブロック中の制御フローステートメントを抑制するため、GotoBreakContinueまたはReturnでは、Finallyブロックを抜け出すことはできません。例えば、tryreturn 42を使用したときは、finallyブロックが実行された後に値42が返されます。これらの文を使ってfinallyブロックから抜け出そうとときは、可能であればロード時に、そうでなければランタイム時にエラーとして検出されます。

トレイメニューやExitAppなど、何らかの手段でスクリプトを直接終了させた場合は、Finally文は実行されません。

OTB(One True Brace)スタイルは、オプションでfinally文を使用することができる。事例:

try {
    ...
} finally {
    ...
}

try {
    ...
} catch {
    ...
} else {
    ...
} finally {
    ...
}

Try, Catch, Else, Throw, Blocks

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 ; hide the 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"
}