</head> <body> <h1>Finally <span class="ver">[v1.1.14+]</span></h1> <p>Ensures that one or more statements are always executed after a <a href="Try.htm">Try</a> statement finishes.</p> <pre class="Syntax"><span class="func">Finally</span> <i>Statement</i></pre> <pre class="Syntax" style="line-height: 100%"> <span class="func">Finally</span> { <i>Statements</i> } </pre> <h2 id="Remarks">Remarks</h2> <p>Every use of <em>finally</em> must belong to (be associated with) a <a href="Try.htm">try</a> (or <a href="Catch.htm">catch</a>) statement above it. A <em>finally</em> always belongs to the nearest unclaimed <em>try</em> statement above it unless a <a href="Block.htm">block</a> is used to change that behavior.</p> <p><em>Try</em> statements behave differently depending on whether <em>catch</em> or <em>finally</em> is present. For more information, see <a href="Try.htm">Try</a>.</p> <p><em>Goto</em>, <em>break</em>, <em>continue</em> and <em>return</em> cannot be used to exit a <em>finally</em> block, as that would require suppressing any control flow instructions within the <em>try</em> block. For example, if <em>try</em> uses <code>return 42</code>, the value 42 is returned after the finally block executes. Attempts to jump out of a <em>finally</em> block using one of these commands are detected as errors at load time where possible, or at run time otherwise.</p> <p>Prior to <span class="ver">[v1.1.19.02]</span>, a bug existed which prevented control flow statements within <em>try</em> from working when <em>finally</em> was present. <em>Return</em> was erroneously permitted within <em>finally</em>, but was ignored if an exception had been thrown.</p> <p><em>Finally</em> statements are not executed if the script is directly terminated by any means, including the tray menu, <a href="ExitApp.htm">ExitApp</a>, or <a href="Exit.htm">Exit</a> (when the script is not <a href="_Persistent.htm">persistent</a>). However, if only the current <a href="../misc/Threads.htm">thread</a> (not the entire script) is exiting, <em>finally</em> statements are executed.</p> <p>The <a href="Block.htm#otb">One True Brace (OTB) style</a> may optionally be used with the <em>finally</em> command. Por exemplo:</p> <pre>try { ... } finally { ... } try { ... } catch e { ... } finally { ... }</pre> <h2 id="Related">Tópicos relacionados</h2> <p><a href="Try.htm">Try</a>, <a href="Catch.htm">Catch</a>, <a href="Throw.htm">Throw</a>, <a href="Block.htm">Blocks</a></p> <h2 id="Examples">Exemplos</h2> <div class="ex" id="ExBasic"> <p><a class="ex_number" href="#ExBasic"></a> Demonstrates the behavior of <em>finally</em> in detail.</p> <pre>try { ToolTip, Working... Example1() } catch e { <em>; For more detail about the object that e contains, see <a href="Catch.htm">Catch</a>.</em> MsgBox, 16,, % "Exception thrown!`n`nwhat: " e.what "`nfile: " e.file . "`nline: " e.line "`nmessage: " e.message "`nextra: " e.extra } finally { ToolTip <em>; hide the tooltip</em> } MsgBox, Done! <em>; This function has a Finally block that acts as cleanup code</em> Example1() { try Example2() finally MsgBox, This is always executed regardless of exceptions } <em>; This function fails when the minutes are odd</em> Example2() { if Mod(A_Min, 2) throw Exception("Test exception") MsgBox, Example2 did not fail }</pre> </div> </body> </html>