{"id":311,"date":"2018-02-04T17:52:37","date_gmt":"2018-02-04T17:52:37","guid":{"rendered":"http:\/\/cppdepend.com\/blog\/?p=311"},"modified":"2023-05-31T16:39:12","modified_gmt":"2023-05-31T16:39:12","slug":"to-use-or-not-to-use-cpp-exceptions-debate","status":"publish","type":"post","link":"https:\/\/cppdepend.com\/blog\/to-use-or-not-to-use-cpp-exceptions-debate\/","title":{"rendered":"To Use or Not To Use: C++ Exceptions Debate"},"content":{"rendered":"<p>More that 20 years ago, the exception handling was added to C++, and after many years of using this feature by the C++ developers, we have a very interesting feedback of their pros and cons.<\/p>\n<p>Let\u2019s discover the opinion of some C++ actors, and what they think about the use of the exception mechanism.<!--more--><\/p>\n<p><strong>I- C++ experts<\/strong><\/p>\n<p><strong><em>Herb Sutter From its article:<\/em><\/strong><\/p>\n<p>The summary: Prefer using exceptions over error codes to report errors. Use status codes (such as return codes or errno) for errors only when exceptions cannot be used (when you don\u2019t control all possible calling code and can\u2019t guarantee it will be written in C++ and compiled using the same compiler and compatible compile options so that exception handling will work), and for conditions that are not errors. Use other methods, such as graceful or ungraceful termination, when recovery is not required or is impossible.<\/p>\n<p><strong><em>Bjarne stroustrup from its FAQ page:<\/em><\/strong><\/p>\n<p>What good can using exceptions do for me? The basic answer is: Using exceptions for error handling makes you code simpler, cleaner, and less likely to miss errors. But what\u2019s wrong with \u201cgood old errnoand if-statements\u201d? The basic answer is: Using those, your error handling and your normal code are closely intertwined. That way, your code gets messy and it becomes hard to ensure that you have dealt with all errors (think \u201cspaghetti code\u201d or a \u201crat\u2019s nest of tests\u201d).<\/p>\n<p><strong><em>Scott Meyers from its \u201cEffective C++\u201d book:<\/em><\/strong><\/p>\n<p>Forty years ago, goto-laden code was considered perfectly good practice. Now we strive to write structured control flows. Twenty years ago, globally accessible data was considered perfectly good practice. Now we strive to encapsulate data. Ten years ago, writing functions without thinking about the impact of exceptions was considered good practice. Now we strive to write ex -ception-safe code.<\/p>\n<p><strong><em>Joel Spolsky from its article:<\/em><\/strong><\/p>\n<p>People have asked why I don\u2019t like programming with exceptions. In both Java and C++, my policy is:<\/p>\n<p>1 \u2013 Never throw an exception of my own<br \/>\n2 \u2013 Always catch any possible exception that might be thrown by a library I\u2019m using on the same line as it is thrown and deal with it immediately.<\/p>\n<p><strong><em>Andrey Andrescu from its article<\/em><\/strong><\/p>\n<p>Writing Exception-Safe Code Is Hard<\/p>\n<p>And later in the same article, he explains how using exceptions could be simplified using the scope guard idiom:<\/p>\n<p>ScopeGuard is a generalization of a typical implementation of the \u201cinitialization is resource acquisition\u201d C++ idiom. The difference is that ScopeGuard focuses only on the cleanup part \u2014 you do the resource acquisition, and ScopeGuard takes care of relinquishing the resource. (In fact, cleaning up is arguably the most important part of the idiom.)<\/p>\n<p><strong><em>Jhon Kalb from its website<\/em><\/strong><\/p>\n<p>Safe usage of exceptions is a non-trivial problem that the industry has struggled with for the better part of two decades. If you have fear, uncertainty, or doubt about exception safety or just want to see the best practices for using exceptions in C++, this session is for you. We\u2019ll start with \u201cWhat is the problem we are trying to solve?\u201d and discuss alternatives, acknowledge the challenges associated with exception usage, and cover some well-meaning but misguided attempts at safety. I will then present a set of guidelines that are the basis for safe exception usage and solid implementation techniques, including how to transition from an exception-unsafe legacy code base.<\/p>\n<p><strong>II- Real projects:<\/strong><\/p>\n<p><strong><em>From the Boost style guide:<\/em><\/strong><\/p>\n<p>When should I use exceptions?<\/p>\n<p>The simple answer is: \u201cwhenever the semantic and performance characteristics of exceptions are appropriate.\u201d<\/p>\n<p><strong><em>From the google style guide<\/em><\/strong><\/p>\n<p>On their face, the benefits of using exceptions outweigh the costs, especially in new projects. However, for existing code, the introduction of exceptions has implications on all dependent code. If exceptions can be propagated beyond a new project, it also becomes problematic to integrate the new project into existing exception-free code. Because most existing C++ code at Google is not prepared to deal with exceptions, it is comparatively difficult to adopt new code that generates exceptions.<\/p>\n<p><strong><em>Clang<\/em><\/strong><\/p>\n<p>The Clang team decide to not use the C++ exceptions, here\u2019s a citation from their coding standard:<\/p>\n<p>In an effort to reduce code and executable size, LLVM does not use RTTI (e.g. dynamic_cast&lt;&gt;) or exceptions. These two language features violate the general C++ principle of \u201cyou only pay for what you use\u201d, causing executable bloat even if exceptions are never used in the code base, or if RTTI is never used for a class. Because of this, we turn them off globally in the code.<\/p>\n<p>In the other side LibreOffice uses the C++ exception mechanism.<\/p>\n<p>When exploring many known C++ open source projects, there\u2019s no preferable choice. The two approaches are widely used.<\/p>\n<p><strong>III- C++ community<\/strong><\/p>\n<p>If we search in forums, articles and discussions in social web sites, we can enumerate three major opinions from the C++ community :<\/p>\n<ul>\n<li>Using them, It\u2019s the best way to handle exceptional circumstances.<\/li>\n<li>Using them but be very careful, they have some side effects.<\/li>\n<li>Never using them.<\/li>\n<\/ul>\n<p>As C++ developer I\u2019m very confused. Should I use Them or not?<\/p>\n<p>There\u2019s one common big issue invoked by all the C++ actors, it\u2019s not trivial to write an exception safety code. We can classify C++ developers in four categories:<\/p>\n<p>Category 1: Use C++ exceptions without care of exception safety code.<\/p>\n<p>Category 2: Use C++ exceptions, and are aware of the safety code problematic.<\/p>\n<p>Category 3: Not Use them but not know exactly the pros and cons of exceptions.<\/p>\n<p>Category 4: Not Use them because they understand exactly the cons of using them.<\/p>\n<p>The developers from categories 2 and 4, know exactly what they do. However, for the categories 1 and 3 they don\u2019t master the exception mechanism, which could influence the quality of their code. Maybe it\u2019s a false debate to discuss the uses of exceptions, what\u2019s more important is : Did you know the pros and cons of using them? It\u2019s up to you after to make a choice depending on your specific constraints. No one could blame you if you choose one of the two approaches.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>More that 20 years ago, the exception handling was added to C++, and after many years of using this feature by the C++ developers, we have a very interesting feedback of their pros and cons. Let\u2019s discover the opinion of some C++ actors, and what they think about the use of the exception mechanism.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[13,40],"class_list":["post-311","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-cpp","tag-exceptions"],"_links":{"self":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/311","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/comments?post=311"}],"version-history":[{"count":4,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/311\/revisions"}],"predecessor-version":[{"id":1499,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/311\/revisions\/1499"}],"wp:attachment":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/media?parent=311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/categories?post=311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/tags?post=311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}