{"id":11480,"date":"2015-06-12T15:59:51","date_gmt":"2015-06-12T19:59:51","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=11480"},"modified":"2015-06-12T22:30:35","modified_gmt":"2015-06-13T02:30:35","slug":"swift-2-error-handling-in-practice","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2015\/06\/12\/swift-2-error-handling-in-practice\/","title":{"rendered":"Swift 2 Error Handling in Practice"},"content":{"rendered":"<p><a href=\"http:\/\/owensd.io\/2015\/06\/12\/swift-throws-get-it.html\">David Owens II<\/a>:<\/p>\n<blockquote cite=\"http:\/\/owensd.io\/2015\/06\/12\/swift-throws-get-it.html\"><p>It took me a little while for me to really appreciate the design of <code>throws<\/code>. At first, it was sheer disbelief that the Swift team created exceptions. Then it was mild irritation that a <code>Result&lt;T, E&gt;<\/code> type wasn&rsquo;t used.<\/p><\/blockquote>\n<p><a href=\"http:\/\/www.sunsetlakesoftware.com\/2015\/06\/12\/swift-2-error-handling-practice\">Brad Larson<\/a>:<\/p>\n<blockquote cite=\"http:\/\/www.sunsetlakesoftware.com\/2015\/06\/12\/swift-2-error-handling-practice\">\n<p>We aren&rsquo;t the only ones that think Result, or a type like it, is a solid solution for error handling in Swift, so it took me by surprise that the Swift team went in a seemingly different direction with Swift 2&rsquo;s new error model. That is, until I started working with it in my own code and found that it did almost everything my Result-based handling does and did it in a cleaner manner.<\/p>\n<p>Swift 2 provides error handling by the means of several new keywords: do, try, catch, throw, throws. To indicate that a function or method can provide an error, you mark it with throw. Errors cannot be thrown within functions or methods that lack this marking. Similarly, error-throwing functions or methods cannot be used without using the try keyword before them. All errors must either be caught (using do\/catch), or passed upward (at which point the function or method must be marked as throws).<\/p>\n<p>Multiple error-throwing functions can be called in sequence, but the first to throw an error aborts execution at that point. An error being thrown acts more like a return statement than the older-style Objective-C NSExceptions, and plays nice with ARC.<\/p>\n<\/blockquote>\n<p>My initial reaction was that Swift 2&rsquo;s error handling is a bit of a hack, a lot of keywords and compiler magic to not give us real exceptions. But Swift fixes some of the problems with exceptions in other languages. And it fixes the main thing that was wrong with the Objective-C <code>NSError<\/code> model, which is that it was very verbose. Most importantly, it doesn&rsquo;t turn ordinary code into a mess of types and closures, which is what I feared Apple was going to do.<\/p>\n<p>I look at what it does to Larson&rsquo;s code, and I&rsquo;m impressed. And he hasn&rsquo;t even showed what <code>defer<\/code> can do. So, although it&rsquo;s unusual, I think this is going to work out well in practice.<\/p>\n<p>There&rsquo;s still work to be done, though. Swift still can&rsquo;t handle actual exceptions raised by Objective-C code. They&rsquo;re treated as runtime errors that bring down your whole program. So some important Cocoa APIs&mdash;plus anything with assertions&mdash;is off-limits.<\/p>\n<p>See also: the two <a href=\"https:\/\/itunes.apple.com\/us\/book\/id1002622538\">Swift<\/a> <a href=\"https:\/\/itunes.apple.com\/us\/book\/id1002624212\">books<\/a> and my <a href=\"http:\/\/mjtsai.com\/blog\/2015\/06\/09\/wwdc-2015-links\/\">WWDC 2015 Links<\/a> post.<\/p>\n<p>Update (2015-06-12): I am going to miss the automatic stack traces that I would get with either true exceptions or my Objective-C <code>NSError<\/code> propagation macros.<\/p>","protected":false},"excerpt":{"rendered":"<p>David Owens II: It took me a little while for me to really appreciate the design of throws. At first, it was sheer disbelief that the Swift team created exceptions. Then it was mild irritation that a Result&lt;T, E&gt; type wasn&rsquo;t used. Brad Larson: We aren&rsquo;t the only ones that think Result, or a type [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[69,79,31,46,30,857,71,901],"class_list":["post-11480","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-exceptions","tag-ios","tag-languagedesign","tag-mac","tag-nserror","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/11480","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=11480"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/11480\/revisions"}],"predecessor-version":[{"id":11482,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/11480\/revisions\/11482"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=11480"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=11480"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=11480"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}