<div dir="ltr"><div class="markdown-here-wrapper" style=""><p style="margin:0px 0px 1.2em!important">2016-08-06 2:50 GMT+03:00 Fernando Rodríguez <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>>:</p>
<p style="margin:0px 0px 1.2em!important"></p><div class="markdown-here-exclude"><p></p><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>b) do/try/catch</div><div><br></div><div>This allows you to have information about the error, but also causes the newly created object to be "trapped" inside a do block.</div><div><br></div><div>Are there any plans to address this situation? I believe there should be a single, obvious and convenient way of handling errors in the language.</div></div></blockquote><p></p></div><p style="margin:0px 0px 1.2em!important"></p>
<pre style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;font-size:1em;line-height:1.2em;margin:1.2em 0px"><code class="hljs language-swift" style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-radius:3px;display:inline;background-color:rgb(248,248,248);white-space:pre;overflow:auto;border-radius:3px;border:1px solid rgb(204,204,204);padding:0.5em 0.7em;display:block!important;display:block;overflow-x:auto;padding:0.5em;color:rgb(51,51,51);background:rgb(248,248,248)"><span class="hljs-func"><span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">func</span> <span class="hljs-title" style="color:rgb(153,0,0);font-weight:bold">attempt</span><span class="hljs-generics"><T></span><span class="hljs-params">(<span class="hljs-number" style="color:rgb(0,128,128)">_</span> expression: @autoclosure <span class="hljs-params">()</span> throws -> T, handler: <span class="hljs-params">(ErrorProtocol)</span> -> T)</span> -> <span class="hljs-title" style="color:rgb(153,0,0);font-weight:bold">T</span> </span>{
<span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">do</span> {
<span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">return</span> try expression()
} catch {
<span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">return</span> handler(error)
}
}
<span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">let</span> x = attempt(downloadDataOrThrow()) {
<span class="hljs-built_in" style="color:rgb(0,134,179)">print</span>(<span class="hljs-string" style="color:rgb(221,17,68)">"Connection lost"</span>)
<span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">return</span> <span class="hljs-built_in" style="color:rgb(0,134,179)">nil</span>
}
</code></pre>
<p style="margin:0px 0px 1.2em!important">It mimics <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-radius:3px;display:inline;background-color:rgb(248,248,248)">guard</code> statement. You can also have versions of <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-radius:3px;display:inline;background-color:rgb(248,248,248)">attempt</code> with partially defined handlers, like “return <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-radius:3px;display:inline;background-color:rgb(248,248,248)">nil</code>“ or “<code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-radius:3px;display:inline;background-color:rgb(248,248,248)">print</code> and crash”, or “assert type of error”.</p>
<div title="MDH:PGRpdiBjbGFzcz0iZ21haWxfZXh0cmEiPjxkaXYgY2xhc3M9ImdtYWlsX3F1b3RlIj4yMDE2LTA4
LTA2IDI6NTAgR01UKzAzOjAwIEZlcm5hbmRvIFJvZHLDrWd1ZXogPHNwYW4gZGlyPSJsdHIiPiZs
dDs8YSBocmVmPSJtYWlsdG86c3dpZnQtZXZvbHV0aW9uQHN3aWZ0Lm9yZyIgdGFyZ2V0PSJfYmxh
bmsiPnN3aWZ0LWV2b2x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