{"id":81,"date":"2006-02-18T10:00:30","date_gmt":"2006-02-18T14:00:30","guid":{"rendered":"http:\/\/benjamin.smedbergs.us\/blog\/2006-02-18\/exceptions-dont-solve-the-problem\/"},"modified":"2006-02-18T11:03:32","modified_gmt":"2006-02-18T15:03:32","slug":"exceptions-dont-solve-the-problem","status":"publish","type":"post","link":"http:\/\/benjamin.smedbergs.us\/blog\/2006-02-18\/exceptions-dont-solve-the-problem\/","title":{"rendered":"Exceptions Don&#8217;t Solve the Problem (and create worse problems)"},"content":{"rendered":"<p><a href=\"http:\/\/weblogs.mozillazine.org\/roadmap\/\">Brendan<\/a> claims that the <a href=\"http:\/\/weblogs.mozillazine.org\/roadmap\/archives\/009727.html\">XPCOM nsresult return type is a significant cause of bloat<\/a> due the need to rv-check every XPCOM method call, and proposes that XPCOM switch to use C++ exceptions. This is an idea that might appear attractive on the surface but would not only break the existing binary compatibility of XPCOM but would permanently destroy XPCOM as a component model with a standard binary API.<!--more--><\/p>\n<p>XPCOM is a component system that is based on <strong>binary<\/strong> compatibility of interfaces. While the layout of interface vtables is based on C++ vtable layout, it does not depend on the peculiarities of the C++ ABI including the layout of concrete classes or how RTTI is calculated or how exception handling is implemented.<\/p>\n<p>Switching XPCOM component code to expect and use exceptions would immediately break not only any existing XPCOM binaries, and force a revision of the XPCOM ABI to version 2, but would also require a careful rewrite of the source code of these components. This would have the affect of breaking XPCOM&#8217;s current binary compatibility with MSCOM. Neither of these are automatic dealbreakers, thought they should be considered extremely carefully.<\/p>\n<p>But exceptions have other side-affects which should be dealbreakers: the implementation of C++ exceptions is extremely dependent on the compiler implementation, both of exceptions themselves and of the underlying RTTI needed to handle them correctly. These implementation details differ between &#8220;minor&#8221; compiler versions such as MSVC6, 7, 7.1 and 8, as well as GCC 3.2, 3.4, and 4.0.<\/p>\n<p>Finally, it is <a href=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2005\/01\/14\/352949.aspx\">much harder to write and read good exception-based code<\/a> than it is to write good nsresult-based code. This has shown up time and time again in Mozilla&#8217;s JS code: clean-looking and carefully-reviewed code lands which has unexpected error conditions: often the unexpected exception occurs from within a catch block.<\/p>\n<p>Result codes (nsresult) encourage coders to make an explicit decision to ignore or handle failure conditions, and make the exit paths from a function body clear(er). <a href=\"http:\/\/www.joelonsoftware.com\/items\/2003\/10\/13.html\">Exceptions are hidden gotos<\/a>: it is possible to write good exception-based code, but it is much harder to write (and perhaps more importantly, read).<\/p>\n<p>Finally, it would take a significant amount of work to instrument the actual effects that exceptions might have on codesize and runtime excution. Simply enabling c++ exception handling and RTTI and disabling the NS_ENSURE_* macros is useless, because try\/catch\/finally blocks are required to properly handle flow control, and they typically have a significant cost in terms of stack-unwind code. I doubt that in the end exception-handling code would be significantly smaller or more efficient than result-handling code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Brendan claims that the XPCOM nsresult return type is a significant cause of bloat due the need to rv-check every XPCOM method call, and proposes that XPCOM switch to use C++ exceptions. This is an idea that might appear attractive on the surface but would not only break the existing binary compatibility of XPCOM but [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-81","post","type-post","status-publish","format-standard","hentry","category-mozilla"],"_links":{"self":[{"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/posts\/81","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/comments?post=81"}],"version-history":[{"count":0,"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/posts\/81\/revisions"}],"wp:attachment":[{"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/media?parent=81"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/categories?post=81"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/tags?post=81"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}