Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As you can probably gather from the wealth of answers, there is certainly no consensus.</p> <p>Semantically, exceptions and error provide the exact same functionality. Indeed they are identical in about all semantic aspects, and errors can be arbitrarily enriched much like exceptions (you don't have to use a simple code, you can use a real bundle of data!).</p> <p>The only difference there is is their propagation methods:</p> <ul> <li>errors have to be passed down manually</li> <li>exceptions are propagated automatically</li> </ul> <p>On the other hand:</p> <ul> <li>the possibility of an error is perfectly documented in the signature</li> <li>exceptions are <em>silent</em> on code inspection (read <a href="http://www.gotw.ca/gotw/020.htm" rel="noreferrer">GotW #20: Code Complexity</a> and cry) and <em>hidden</em> paths of execution make reasoning harder.</li> </ul> <p>The very reason both solutions can appear clunky is simply that error checking <em>is difficult</em>. Indeed most of the code I am writing daily concerns error checking, whether technical or functional.</p> <p><strong>So what to do ?</strong></p> <p><em>Warning: demonstration ahead, jump over to the next section if you care only for an answer</em></p> <p>I personally like to leverage the type system here. The typical example is the pointer-reference dichotomy: a pointer is like a reference that can be null (and reseated, but it does not matter here)</p> <p>Therefore, instead of:</p> <pre><code>// Exceptions specifications are better not used in C++ // Those here are just to indicate the presence of exceptions Object const&amp; Container::search(Key const&amp; key) const throw(NotFound); </code></pre> <p>I will tend to write:</p> <pre><code>Object const* Container::search(Key const&amp; key) const; </code></pre> <p>Or better yet, using <em>clever</em> pointers:</p> <pre><code>Pointer&lt;Object const&gt; Container::search(Key const&amp; key) const; template &lt;typename O&gt; O* Pointer&lt;O&gt;::operator-&gt;() const throw(Null); template &lt;typename O&gt; O&amp; Pointer&lt;O&gt;::operator*() const throw(Null); </code></pre> <p>Here I find the use of exception superfluous for 2 reasons:</p> <ul> <li>If we are searching for an object, then there not finding it is both a perfectly common occurrence and there is not much data to carry about: cause of error ? <em>it is not there</em></li> <li>The client does not necessarily consider it an error that it is not there, who am I to assume that I know her business better than she does ? Who am I to decide that there will <em>never</em> be a case where it won't be appropriate not to find what was asked for ?</li> </ul> <p>I don't have a problem with exceptions per se, but they can make the code awkward, consider:</p> <pre><code>void noExceptions(Container const&amp; c) { Pointer&lt;Object const&gt; o = c.search("my-item"); if (!o) { o = c.search("my-other-item"); } if (!o) { return; } // nothing to be done // do something with o } </code></pre> <p>And compare it with the "exception" case:</p> <pre><code>void exceptions(Container const&amp; c) { Object const* p = 0; try { p = &amp;c.search("my-item"); } catch(NotFound const&amp;) { try { p = &amp;c.search("my-other-item"); } catch(NotFound const&amp;) { return; // nothing to be done } } // do something with p } </code></pre> <p>In this case, the use of exceptions does not seem appropriate :/</p> <p>On the other hand:</p> <pre><code>try { print() &lt;&lt; "My cute little baby " &lt;&lt; baby.name() &lt;&lt; " weighs " &lt;&lt; baby.weight(); } catch(Oupsie const&amp;) { // deal } </code></pre> <p>is certainly more appealing than:</p> <pre><code>if (!print("My cute little baby ")) { /*deal*/ } if (!print(baby.name())) { /*deal*/ } if (!print(" weighs ")) { /*deal*/ } if (!print(baby.weight())) { /*deal*/ } </code></pre> <hr> <p><strong>What is the best then ?</strong></p> <p>It depends. Like all engineering problem there is no silver bullet, it's all about concessions.</p> <p>So keep 2 things in mind:</p> <ul> <li><em>Error reporting is part of the API</em></li> <li><em>APIs should be designed with ease of use in mind</em></li> </ul> <p>If you find yourself wondering whether to use an exception or not, just try to use your API. If there is no clear cut winner, it is just that: there is no ideal solution.</p> <p>Oh, and do not hesitate to refactor your API when it becomes clear that the error reporting mechanism elected at the time of crafting it is no longer appropriate. Don't be ashamed: requirements change with time, so it is normal that the API change with them.</p> <p><em>Personally</em> I tend to use exceptions for unrecoverable errors only: I therefore have few try/catch in my code, only in the outermost levels, to accurately log the error (love stack frames) and log a dump of the BOM as well.</p> <p>This is very similar (and indeed strongly influenced) by Haskell, the code there is seggregated in two clear cut parts: while any can throw exceptions, only the IO part (the extern one) may actually catch them. Therefore, the pure part must deal with error conditions with other ways in case they are "normal".</p> <p>If, however, I am faced with a problem where using an exception makes the code easier to read and more <em>natural</em> (which is subjective) then I use an exception :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload