Finally

Auto Hotkey

Finally

Ensures that one or more statements (commands or expressions) are always executed after the execution of a try statement.

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).

The One True Brace (OTB) style may optionally be used with the finally command. For example:

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

try {
    ...
} catch e {
    ...
} finally {
    ...
}

Related

Try, Catch, Throw, Blocks

Examples

try
{
    ToolTip, Working...
    Example1()
}
catch e
{
    ; For more detail about the object that e contains, see Catch.
    MsgBox("Exception 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!

; 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
}