<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">On Feb 29, 2016, at 12:09 PM, Jacob Bandes-Storch via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><div><blockquote type="cite" class=""><b class="">I propose extending guard-statements to handle errors</b> without using the optional "try?" and without a do-block, by allowing the expression to throw, and offering "catch" instead of "else":<br class=""><div class=""><div dir="ltr" class=""><div class=""><br class=""></div><div class="">&nbsp; &nbsp; // func foo() throws -&gt; T ...</div><div class="">&nbsp; &nbsp; guard let x = <b class="">try</b> foo <b class="">catch</b> {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; print("the error was: \(<b class="">error</b>)") &nbsp;// the "error" parameter is available here</div>&nbsp; &nbsp; &nbsp; &nbsp; // the compiler does not allow control flow to escape this block<div class="">&nbsp; &nbsp; }</div>&nbsp; &nbsp; // control flow cannot reach here if foo() threw an error</div></div></blockquote><br class=""></div><div>I don’t think that this syntax makes sense, because you’re changing "guard let x = ” to not test an optional. &nbsp;The syntax you’re searching for seems more consistent as a modifier on var/let itself:</div><div><br class=""></div><div>// func foo() throws -&gt; T ...</div><div>let x =&nbsp;try&nbsp;foo()&nbsp;catch&nbsp;{<br class="">&nbsp; &nbsp; &nbsp; &nbsp; print("the error was: \(error)") &nbsp;// the "error" parameter is available here<br class="">&nbsp; &nbsp; &nbsp; &nbsp; // the compiler does not allow control flow to escape this block<br class="">}</div><div><br class=""></div><div>The guard form of this would still make sense, but the existing “guard let” and “guard case” matching should work as it does. &nbsp;For example, something like this should be allowed:</div><div><br class=""></div><div><div>// func bar() throws -&gt; T?</div><div>guard let x =&nbsp;try&nbsp;bar() else {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; // this runs if ‘bar’ returns nil.</div>&nbsp; &nbsp; &nbsp; &nbsp; // the compiler does not allow control flow to escape this block<div>} catch&nbsp;{<br class="">&nbsp; &nbsp; &nbsp; &nbsp; print("the error was: \(error)") &nbsp;// the "error" parameter is available here<br class="">&nbsp; &nbsp; &nbsp; &nbsp; // the compiler does not allow control flow to escape this block<br class="">}</div><div class=""><br class=""></div><div class="">More generally, the “guard” form would be workable on anything that takes a stmt-condition. &nbsp;This brings “if" and “while” into the mix.</div><div class=""><br class=""></div><div class="">-Chris</div></div></body></html>