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ブロック中の制御フローステートメントを抑制するため、Goto、Break、ContinueまたはReturnでは、Finallyブロックを抜け出すことはできません。例えば、tryが return 42
を使用したときは、finallyブロックが実行された後に値42が返されます。これらの文を使ってfinallyブロックから抜け出そうとときは、可能であればロード時に、そうでなければランタイム時にエラーとして検出されます。
トレイメニューやExitAppなど、何らかの手段でスクリプトを直接終了させた場合は、Finally文は実行されません。
OTB(One True Brace)スタイルは、オプションでfinally文を使用することができる。事例:
try { ... } finally { ... } try { ... } catch { ... } else { ... } finally { ... }
Try, Catch, Else, Throw, Blocks
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" }