</head> <body> <h1>Try</h1> <p>守护一个或多个<a href="../Concepts.htm#statement">语句</a>不受由 <a href="Throw.htm">Throw</a> 语句抛出的运行时错误和值的影响.</p> <pre class="Syntax"><span class="func">Try</span> <i>Statement</i></pre> <pre class="Syntax" style="line-height: 100%"><span class="func">Try</span> { <i>Statements</i> }</pre> <h2 id="Remarks">备注</h2> <p><em>Try</em> 语句后常跟着<a href="Block.htm">区块</a>, 括在大括号中的一个或多个<a href="../Concepts.htm#statement">语句</a>. 如果仅需执行单个语句, 那么此语句可以和 <em>Try</em> 放在同一行, 也可以在下一行, 并且可以省略大括号. 要指定仅在 <em>Try</em> 捕获错误时执行的代码, 请使用一个或多个 <a href="Catch.htm">Catch</a> 语句.</p> <p>如果使用 <em>Try</em> 而没有 <em>Catch</em> 或 <em>Finally</em>, 那就相当于一个空的 <code>catch Error</code>.</p> <p><a href="Throw.htm">Throw</a> 语句或程序在运行遇到错误时会抛出值. 当 <em>Try</em> 区块或由其调用的函数抛出值时, 将进行下列操作:</p> <ul> <li>如果该区块有相应的 <a href="Catch.htm">Catch</a> 语句, 则继续执行该语句.</li> <li>如果没有匹配的 <a href="Catch.htm">Catch</a> 语句但有 <a href="Finally.htm">Finally</a> 语句, 则执行该语句并在结束后自动再次抛出值.</li> <li>如果没有匹配的 <em>Catch</em> 语句或 <em>Finally</em> 语句, 值会被自动再次抛出(除非根本没有 <em>Catch</em> 或 <em>Finally</em>, 如上所述).</li> </ul> <p>最后一个 <em>Catch</em> 后面可以选择加上 <em>Else</em>. 如果 <em>Try</em> 语句完成后没有抛出异常(并且没有通过 <em>Return</em>, <em>Break</em> 或类似的方式将控制权转移到其他地方), 则会执行 <em>Else</em> 语句. 由 <em>Else</em> 语句抛出的异常不会被其相关的 <em>Try</em> 语句处理, 但可以被一个包围的 <em>Try</em> 语句处理. <em>Finally</em> 也可以出现, 但是必须放在 <em>Else</em> 之后, 而且总是最后执行.</p> <p>如果在没有 try 区块执行的情况下引发异常, 则会显示错误消息, 并且当前线程退出.</p> <p>可以选择将 <a href="Block.htm#otb">One True Brace(OTB) 样式</a>与 <em>try</em> 语句一起使用. 例如:</p> <pre>try { ... } catch Error as err { ... } else { ... } finally { ... }</pre> <h2 id="Related">相关</h2> <p><a href="Throw.htm">Throw</a>, <a href="Catch.htm">Catch</a>, <a href="Else.htm">Else</a>, <a href="Finally.htm">Finally</a>, <a href="Block.htm">区块</a>, <a href="OnError.htm">OnError</a></p> <h2 id="Examples">示例</h2> <div class="ex" id="ex_basic"> <p><a class="ex_number" href="#ex_basic"></a> 演示 Try/Catch/Throw 的基本概念.</p> <pre>try <em>; 尝试执行的代码.</em> { HelloWorld MakeToast } catch as e <em>; 处理由上面区块抛出的首个错误.</em> { MsgBox "An error was thrown!`nSpecifically: " e.Message Exit } HelloWorld() <em>; 总是成功.</em> { MsgBox "Hello, world!" } MakeToast() <em>; 总是失败.</em> { <em>; 立即跳到 try 区块的错误处理程序:</em> <a href="Throw.htm">throw</a> Error(A_ThisFunc " is not implemented, sorry") } </pre> </div> <div class="ex" id="ex_el"> <p><a class="ex_number" href="#ex_el"></a> 演示内置函数的基本错误处理.</p> <pre>try { <em>; 下列语句尝试备份某些指定类型的文件:</em> FileCopy A_MyDocuments "\*.txt", "D:\Backup\Text documents" FileCopy A_MyDocuments "\*.doc", "D:\Backup\Text documents" FileCopy A_MyDocuments "\*.jpg", "D:\Backup\Photos" } catch { MsgBox "There was a problem while backing the files up!",, "IconX" ExitApp 1 } else { MsgBox "Backup successful." ExitApp 0 } </pre> </div> <div class="ex" id="ex_com"> <p><a class="ex_number" href="#ex_com"></a> 演示使用 <em>Try-Catch</em> 处理 COM 错误. 有关下面使用的 COM 对象的详情, 请参阅 <a href="https://learn.microsoft.com/previous-versions/visualstudio/visual-studio-6.0/aa227633(v=vs.60)">Using the ScriptControl (Microsoft Docs)</a>.</p> <pre>try { obj := ComObject("ScriptControl") obj.ExecuteStatement('MsgBox "This is embedded VBScript"') <em>; 这一行会产生一个 Error.</em> obj.InvalidMethod() <em>; 这一行会产生一个 MethodError.</em> } catch MemberError <em>; 包括 MethodError 和 PropertyError.</em> { MsgBox "We tried to invoke a member that doesn't exist." } catch as e { <em>; 关于 e 对象的更多细节, 请参阅 <a href="Error.htm">Error 对象</a>.</em> MsgBox("Exception thrown!`n`nwhat: " e.what "`nfile: " e.file . "`nline: " e.line "`nmessage: " e.message "`nextra: " e.extra,, 16) } </pre> </div> <div class="ex" id="ex_nesting"> <p><a class="ex_number" href="#ex_nesting"></a> 演示嵌套的 <em>Try-Catch</em> 语句.</p> <pre>try Example1 <em>; 单个语句可以和 Try 放在同一行.</em> catch Number as e MsgBox "Example1() threw " e Example1() { try Example2 catch Number as e { if (e = 1) throw <em>; 将捕获的值重新抛给调用者.</em> else MsgBox "Example2() threw " e } } Example2() { throw Random(1, 2) }</pre> </div> </body> </html>