Finally

提供: AutoHotkey Wiki
移動: 案内検索

実行制御 | GUI表示 | 演算・変数 | メモリ・DLL操作 | 文字列操作 | キーボード | マウス | シェル | ウィンドウ | ウィンドウグループ
ステータスバー | コントロール | サウンド | ファイル | INIファイル | レジストリ | 環境変数 | AutoHotkey | その他 | 設定関係 | オブジェクト

Finally [v1.1.14+][編集]

Tryの実行後、1つ以上の「コマンド/」を実行する。

Finally Statement
Finally
{
	Statements
}

Remarks[編集]

Every use of finally must belong to (be associated with) a try (or catch) statement above it. A finally always belongs to the nearest unclaimed try statement above it unless a block is used to change that behavior.

If a finally statement is applied to a try statement with no catch block, the exception is not absorbed by the latter and exception propagation continues after the execution of the finally statement.

It is not allowed to use goto/break/continue in order to exit a finally statement, as that would imply breaking exception propagation (however, normal usage that keeps execution inside the block is allowed). This mistake is detected at load time (and at run time for dynamic goto statements).

必要に応じて以下のような配置ができる。

Try {
    ...
} Finally {
    ...
}

Try {
    ...
} Catch e {
    ...
} Finally {
    ...
}

Related[編集]

Try, Catch, Throw, Blocks

Example(s)[編集]

Try
{
    ToolTip, Working...
    Example1()
}
Catch e
{
    ; For more detail about the object that e contains, see Catch.
    MsgBox, 16,, % "Exception thrown!`n`nwhat: " e.what "`nfile: " e.file
        . "`nline: " e.line "`nmessage: " e.message "`nextra: " e.extra
}
Finally
{
    ToolTip ; hide the ToolTip
}

MsgBox, Done!

; This function has a Finally block that acts as cleanup code
Example1()
{
    Try
        Example2()
    Finally
        MsgBox, This is always executed regardless of exceptions
}

; This function fails when the minutes are odd
Example2()
{
    If Mod(A_Min, 2)
        Throw Exception("Test exception")
    MsgBox, Example2 did not fail
}