</head> <body> <h1>Try <span class="ver">[v1.1.04+]</span></h1> <p>하나 이상의 서술문 (명령어나 표현식)을 <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>이 따라옵니다 - 활괄호로 둘러 싸인 하나 이상의 서술문 (명령어나 표현식). 하나의 서술문만 실행한다면, <em>try</em>와 같은 줄 또는 다음 줄에 배치할 수 있고, 활괄호를 생략할 수 있습니다. <em>try</em>가 에러를 잡을 때 정확하게 한 번만 실행되는 코드를 지정하려면, <a href="Catch.htm">catch</a> 명령어를 사용하십시오.</p> <p>실행시간 에러가 일어나면 <a href="Throw.htm">throw</a> 명령어 또는 프로그램이 예외를 던질 수 있습니다. try 블록 안에서 또는 블록에서 호출한 함수 안에서 예외가 던져질 때, 다음과 같은 일이 일어납니다:</p> <ul> <li>상응하는 <a href="Catch.htm">catch</a> 서술문이 있다면, 실행은 거기에서 계속됩니다.</li> <li>catch 서술문은 없지만 <a href="Finally.htm">finally</a> 서술문이 있다면, 그것이 실행됩니다. 그러나 일단 끝나면 예외는 자동으로 다시 던져집니다.</li> <li>catch 서술문도 없고 finally 서술문도 없다면, 실행은 다음 줄 try 블록 밖으로 계속됩니다.</li> </ul> <p>try 블록이 실행 중이지 않은 동안에 예외가 던져지면, 에러 메시지를 보여주고 현재 쓰레드를 종료합니다.</p> <p><a href="Block.htm#otb">One True Brace (OTB) 스타일</a>을 선택적으로 <em>try</em> 명령어에 사용할 수 있습니다. 예를 들어:</p> <pre>try { ... } catch e { ... }</pre> <h2 id="Related">관련 항목</h2> <p><a href="Catch.htm">Catch</a>, <a href="Throw.htm">Throw</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> Demonstrates the basic concept of try/catch/throw.</p> <pre>try <em>; 코드를 실행해 봅니다.</em> { HelloWorld() MakeToast() } <a href="Catch.htm">catch</a> e <em>; 위의 블록에서 일으킨 첫 번째 에러/에외를 처리합니다.</em> { MsgBox, 예외가 던져졌습니다!`n구체적으로 다음과 같습니다: %e% <a href="Exit.htm">Exit</a> } HelloWorld() <em>; 언제나 성공합니다.</em> { MsgBox, Hello, world! } MakeToast() <em>; 언제나 실패합니다.</em> { <em>; try 블록의 에러 처리에 곧바로 점프합니다:</em> <a href="Throw.htm">throw</a> A_ThisFunc " is not implemented, sorry" } </pre> </div> <div class="ex" id="ex_el"> <p><a class="ex_number" href="#ex_el"></a> Demonstrates the use of try/catch instead of ErrorLevel.</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, 16,, 파일을 백업하는 동안 문제가 있었습니다! ExitApp } </pre> </div> <div class="ex" id="ex_com"> <p><a class="ex_number" href="#ex_com"></a> Demonstrates the use of try/catch dealing with COM errors. For details about the COM object used below, see <a href="http://msdn.microsoft.com/ko-kr/library/aa227633(v=vs.60).aspx">Using the ScriptControl (Microsoft Docs)</a>.</p> <pre>try { obj := <a href="ComObjCreate.htm">ComObjCreate</a>("ScriptControl") obj.ExecuteStatement("MsgBox ""This is embedded VBScript""") obj.InvalidMethod() <em>; 이 줄은 실행 시간 에러를 일으킵니다.</em> } catch e { <em>; e를 담고 있는 객체에 관한 더 자세한 정보는 <a href="Throw.htm#Exception">Exception()</a>를 참조하십시오.</em> MsgBox, 16,, % "Exception thrown!`n`nwhat: " e.what "`nfile: " e.file . "`nline: " e.line "`nmessage: " e.message "`nextra: " e.extra } </pre> </div> <div class="ex" id="ex_nesting"> <p><a class="ex_number" href="#ex_nesting"></a> Demonstrates nesting try-catch statements.</p> <pre>try Example1() <em>; 한 줄 서술문은 Try 명령어와 같은 줄에 있을 수 있습니다.</em> catch e MsgBox, Example1() threw %e%. Example1() { try Example2() catch e { if (e = 1) throw e <em>; 예외를 다시 던져서 호출자가 받을 수 있도록 합니다.</em> else MsgBox, Example2() threw %e%. } } Example2() { Random, o, 1, 2 throw o }</pre> </div> </body> </html>