<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=""><br class=""><div><blockquote type="cite" class=""><div class="">On 6 Aug 2016, at 7:50 AM, Fernando RodrÃguez 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="">Hi,<div class=""><br class=""></div><div class="">I do a lot of training and one of the features of Swift that seems more confusing to students (and myself) is error handling. There are too many ways of doing it, and none seems satisfactory.</div><div class=""><br class=""></div><div class="">Let's take for example an initializer. There are 2 different ways of handling errors within an init:</div><div class=""><br class=""></div><div class="">a) Old School: return nil. This was very simple in Objective C, because the uncommon case of an error could be easily ignored...until everything crashed.</div><div class=""><br class=""></div><div class="">In Swift it's not easy (nor advisable IMHO) to completely ignore the possibility of an error. Besides, it has 2 complications.</div><div class=""><br class=""></div><div class="">First of all, you return an Optional, and Optionals have a tendency to go viral. Suddenly, all your code has to deal with optionals.</div><div class=""><br class=""></div><div class="">Secondly, you have no information about the error itself.</div><div class=""><br class=""></div><div class="">b) do/try/catch</div><div class=""><br class=""></div><div class="">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></div></blockquote><div><br class=""></div><div><div>You can define an uninitialised variable outside the scope of `do`, and assign to it within the scope of `do`.</div><div><br class=""></div><div><br class=""></div><div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);" class="">func</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> test() </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);" class="">throws</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> -> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> { </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);" class="">return</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(39, 42, 216);" class="">1</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> }</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; min-height: 16px;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""></span><br class=""></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);" class="">let</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> variable: </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);" class="">Int</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);" class=""><br class=""></span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);" class="">do</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> {</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>variable = </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);" class="">try</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(49, 89, 93);" class="">test</span><span style="font-variant-ligatures: no-common-ligatures;" class="">()</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">} </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);" class="">catch</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);" class="">let</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> error {</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(62, 30, 129);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span></span><span style="font-variant-ligatures: no-common-ligatures;" class="">fatalError</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);" class="">"</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class="">\</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class="">error</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);" class="">)"</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class="">)</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">}</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; min-height: 16px;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""></span><br class=""></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(62, 30, 129);" class="">print</span><span style="font-variant-ligatures: no-common-ligatures;" class="">(</span><span style="color: rgb(79, 129, 135); font-variant-ligatures: no-common-ligatures;" class="">variable</span><span style="font-variant-ligatures: no-common-ligatures;" class="">)</span></div></div></div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class="">What do you guys think?</div><div class=""><br class=""></div><div class=""><br clear="all" class=""><div class=""><br class=""></div>-- <br class=""><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr" class=""><div class=""><br class=""></div><div class=""><div dir="ltr" style="font-family:arial;font-size:small;line-height:normal" class=""><table width="400" border="0" cellspacing="0" cellpadding="0" style="font-family: Times;" class=""><tbody class=""><tr class=""><td width="150" rowspan="3" align="center" valign="top" class=""><br class=""><table border="0" cellspacing="0" cellpadding="0" style="font-family:Times;border-collapse:collapse;border:none" class=""><tbody class=""><tr class=""><td width="112" valign="top" style="width:111.75pt;padding:0cm 5.4pt" class=""><div class=""><br class="webkit-block-placeholder"></div><p class=""><b class=""><span lang="ES" style="font-family:Helvetica;color:rgb(213,117,7)" class=""><a href="http://keepcoding.io/es/" style="color:rgb(17,85,204)" target="_blank" class=""><img src="https://docs.google.com/uc?export=download&id=0Bzx0v_J9FCNPNllTZ19uM1hxbXc&revid=0Bzx0v_J9FCNPb2trZU1BWXFabFdkUngvd0Nmd2Q4ZTdWaHNNPQ" width="200" height="138" class=""></a></span></b></p></td></tr></tbody></table><br class=""><br class=""></td><td style="font-family:Helvetica,Arial,sans-serif;font-size:12px;color:rgb(100,100,100)" class=""><div style="margin: 0px; font-size: 14px; font-family: Helvetica; color: rgb(64, 64, 64);" class=""><b class="">Fernando RodrÃguez</b></div><div style="margin: 0px; font-family: Helvetica;" class="">m. +34 610 965 332</div><div style="margin: 0px; font-family: Helvetica;" class="">t. +34 91 629 57 61</div><div style="margin: 0px; font-family: Helvetica;" class=""><a href="mailto:fernando@agbo.biz" target="_blank" class="">fernando@k</a><a href="http://eepcoding.io/" target="_blank" class="">eepcoding.io</a></div><div style="margin: 0px; font-family: Helvetica;" class=""><span class=""><img alt="pastedGraphic.png" class=""></span><span style="font-size:16px;font-family:Arial;color:rgb(35,35,35)" class=""> </span><img alt="pastedGraphic_1.png" class=""><span style="font-size:16px;font-family:Arial;color:rgb(35,35,35)" class=""> </span><img alt="pastedGraphic_2.png" class=""></div><div style="margin: 0px; font-size: 10px; font-family: Helvetica; color: rgb(64, 64, 64);" class=""><b class=""><a href="http://KeepCoding.io" class="">KeepCoding.io</a></b></div><div style="margin: 0px; font-size: 10px; font-family: Helvetica; color: rgb(64, 64, 64);" class="">2120 University Avenue, Berkeley, CA</div><div style="margin: 0px; font-size: 10px; font-family: Helvetica; min-height: 12px;" class=""><br class=""></div><div style="margin: 0px; font-size: 10px; font-family: Helvetica;" class="">Avda. Fuencarral, 44, Ed. 8, Loft 30</div><div style="margin: 0px; font-size: 10px; font-family: Helvetica;" class="">28108 Alcobendas (Madrid) Spain</div><div class=""><br class=""></div></td></tr></tbody></table></div><div style="font-family: Helvetica;" class=""><div dir="ltr" style="color:rgb(34,34,34);font-family:arial;font-size:small;line-height:normal" class=""><table width="75%" border="0" cellspacing="0" cellpadding="0" style="font-family: Times;" class=""><tbody class=""><tr class=""><td style="font-family:Verdana;font-size:7px;color:rgb(153,153,153);padding-top:10px;text-align:justify" class=""><br class=""></td></tr></tbody></table></div></div></div></div></div>
</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=""></body></html>