{"id":1379,"date":"2007-01-07T14:32:36","date_gmt":"2007-01-07T18:32:36","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/2007\/01\/07\/nsxreturnthrowerror\/"},"modified":"2014-07-14T10:17:27","modified_gmt":"2014-07-14T14:17:27","slug":"nsxreturnthrowerror","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2007\/01\/07\/nsxreturnthrowerror\/","title":{"rendered":"NSXReturnThrowError"},"content":{"rendered":"<p>\r\nJonathan \u201cWolf\u201d Rentzsch has posted some interesting <a href=\"http:\/\/svn.sourceforge.net\/viewvc\/*checkout*\/redshed\/tags\/NSXReturnThrowError-0.9\/html\/index.html\">Cocoa error-wrapping macros<\/a>. I\u2019ve long used some macros to generate and raise <code>NSExceptions<\/code> that have the <code>name<\/code>, <code>reason<\/code>, and <code>userInfo<\/code> set based on the error code, a format, and the location in the source file. Rentzsch\u2019s macros go beyond this to stuff the relevant information into a more modern <code>NSError<\/code> object, with the error domain cleverly deduced from the function\u2019s return type using <code>@encode<\/code> and <code>typeof<\/code>. The <code>NSXThrowError<\/code> macro will also raise an <code>NSException<\/code> that has the <code>NSError<\/code> stuffed into its <code>userInfo<\/code>. Lastly, the actual Objective-C expression that generated the error code is saved as a string in the <code>NSError<\/code>.\r\n<\/p>\r\n\r\n<p>\r\nPutting on my code critiquing hat, I don\u2019t like that <code>NSXThrowError<\/code> introduces an exception name beginning with the reserved <tt>NS<\/tt> prefix or that the function-like macros implicitly assign to an <code>error<\/code> variable in the local scope. Also, they should probably be do-while protected.\r\n<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>Jonathan \u201cWolf\u201d Rentzsch has posted some interesting Cocoa error-wrapping macros. I\u2019ve long used some macros to generate and raise NSExceptions that have the name, reason, and userInfo set based on the error code, a format, and the location in the source file. Rentzsch\u2019s macros go beyond this to stuff the relevant information into a more [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","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,941,857,54,71],"class_list":["post-1379","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-exceptions","tag-macros","tag-nserror","tag-objective-c","tag-programming"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/1379","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=1379"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/1379\/revisions"}],"predecessor-version":[{"id":9145,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/1379\/revisions\/9145"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=1379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=1379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=1379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}