<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="">Both of your proposals seem contrary to the very reasons Swift’s error handling is the way it is, so it’s unlikely they’ll find any support. I’m not aware of any “error handling” manifesto, as Swift 2 was created before the evolution process, but I’m sure someone has something to share about why Swift’s error system is designed this way.<div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Jon</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Mar 2, 2017, at 3:14 AM, Paweł Wojtkowiak via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="">I was writing some code yesterday which utilized throwing funcs and I found some inconveniences which I think could be improved in some future version of Swift.<br class=""></div><div class=""><br class=""></div><div class="">1. <b class="">Default error handling</b></div><div class="">Swift, unlike most languages, requires us to put try, try? or try! before calling throwing funcs. When using try, we already have our do..catch block surrounding the throwing func. Sometimes there are multiple cases where the error is handled the same way, and there's a need to write the same do..catch blocks every time, or just intercept the error in the throwing func or a wrapping function for a throwing one, but then there is no possibility to do the handling anymore if we want to handle it another way in some specific case. I thought that this would be a good idea to introduce something like default error handling e.g. with calling the func without using the try keyword or do..catch block. In this case, a keyword like throws? could be introduced to be used to use instead of throws when defining a function. Such a function would:</div><div class="">1. Contain do..catch blocks, like normal throwing func</div><div class="">2. Would execute these do..catch blocks if the caller does not use a variant of try when calling the func</div><div class="">3. Would not execute these do..catch blocks and pass the error to the caller, the same way as the func would do if there would be no do..catch blocks and the function was marked with "throws". In this case, the caller would be the one to handle the error.</div><div class=""><br class=""></div><div class="">e.g.</div><div class=""><br class=""></div><div class="">enum SomeError: Error {</div><div class=""> case myError</div><div class="">}</div><div class=""><br class=""></div><div class="">func justThrowing() throws {</div><div class=""> throw SomeError.myError</div><div class="">}</div><div class=""><br class=""></div><div class="">func myFailableFunc() throws? {</div><div class=""> do {</div><div class=""> try justThrowing()</div><div class=""> } catch {</div><div class=""> print("Handled an error")</div><div class=""> }</div><div class="">}</div><div class=""><br class=""></div><div class="">func testing() {</div><div class=""> </div><div class=""> // This one would print "Handled an error"</div><div class=""> myFailableFunc() </div><div class=""><br class=""></div><div class=""> // This one would handle the error like any other func marked with "throws"</div><div class=""> // try? and try! would also work exactly like for a normal throwing func<br class=""></div><div class=""> do {</div><div class=""> try myFailableFunc()</div><div class=""> } catch {<br class=""> print("Handled this in the caller")</div><div class=""> }<br class=""><br class=""></div><div class="">}<br class=""></div><div class=""><br class=""></div><div class="">2. <b class="">Grouping throwing function calls</b><br class=""></div><div class=""><b class=""><br class=""></b></div><div class="">I think there are cases when people are not too concerned about an error from multiple function calls in a row and what they want is to know if these functions failed or not. This could be, for example, when these operations are just doing some micro-work by calling the same func on different instances of a type and are a part of some larger operation, it is inconvenient to write try, try? or try! before each of them.</div><div class=""><br class=""></div><div class="">Instead, I would suggest introducing do try { ... } catch blocks or simply allowing to use try like this:</div><div class="">try { </div><div class=""> throwingFunc1()</div><div class=""> throwingFunc2()</div><div class=""> throwingFunc3()</div><div class="">}</div><div class=""><br class=""></div><div class="">Now, we're forced to do it like this:</div><div class="">try throwingFunc1()</div><div class="">try throwingFunc2()</div><div class="">try throwingFunc3()</div><div class=""><br class=""></div><div class="">This often becomes inconvenient and looks messy in the code. I also think that this concept could also work for try? and try!.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">What are your opinions on these? Did you have any places in your code where this could be useful?</div><div class="">If you have some better ideas on the concept or implementation, or have some counterarguments to these suggestions, feel free to comment. </div></div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></div></body></html>