_If_ this is something that's important, then the ! should follow 'catch' and not 'try'.<br><br>The keyword 'try!' already means something, and it's not that. People may already mix try! and try in a do block and the behavior cannot be changed for source compatibility.<br><div class="gmail_quote"><div dir="ltr">On Sat, May 6, 2017 at 15:16 Simon Pilkington via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">As a number of recent threads have shown, typed throws and error handling is a complicated topic with a lot of implications.<div><br></div><div>However in the mean time we could potentially address one of the pain points people give for wanting typed throws - exhaustive catch statements.</div><div><br></div><div>For functions that you completely control, it is possible to be completely certain of the errors the function will throw and it is an unrecoverable logical error for any other error to be thrown. Currently this situation would have to be handled like so-</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">do</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(49,89,93)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">try</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> </span><span style="font-variant-ligatures:no-common-ligatures">throwingFunction</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">()</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">} </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">catch</span><span style="font-variant-ligatures:no-common-ligatures"> MyError.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(49,89,93)">TheError</span><span style="font-variant-ligatures:no-common-ligatures"><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">let</span><span style="font-variant-ligatures:no-common-ligatures"> theContext)</span> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> </span><span style="font-variant-ligatures:no-common-ligatures">// do something</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(186,45,162)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">} </span><span style="font-variant-ligatures:no-common-ligatures">catch</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(62,30,129)">fatalError</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">(</span><span style="font-variant-ligatures:no-common-ligatures">“Impossible error?"</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></div></div><div><br></div><div>Here, the catch-all is just boilerplate to make the catch statement exhaustive but doesn’t provide any value to the programmer. Would it make sense to provide a shorthand for this case like this-</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">do</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(49,89,93)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">try</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">! </span><span style="font-variant-ligatures:no-common-ligatures">throwingFunction</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">()</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">} </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">catch</span><span style="font-variant-ligatures:no-common-ligatures"> MyError.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(49,89,93)">TheError</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">let</span><span style="font-variant-ligatures:no-common-ligatures"> theContext) </span>{</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> </span><span style="font-variant-ligatures:no-common-ligatures">// do something</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></div></div><div><br></div><div>Where the <span style="color:rgb(186,45,162);font-family:Menlo;font-size:11px;font-variant-ligatures:no-common-ligatures">try</span><span style="font-family:Menlo;font-size:11px;font-variant-ligatures:no-common-ligatures">!</span> still indicates the call could fatalError if the function throws an unmatched error but allows the programmer to specify the errors that they specifically want to handle. I don’t think this moves the language away from the possibility of typed throws; adding typed throws would likely cause a warning in both cases - either to remove the catch-all or all the ! - if the compiler could determine the catch was now exhaustive without them.</div><div><br></div><div>If this has been proposed elsewhere I apologise. </div><div><br></div><div>Cheers,</div><div>Simon</div></div>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote></div>