    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: Notes on Exceptions</title>
        <link>https://members.accu.org/index.php/articles/586</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">Programming Topics + Overload Journal #28 - Oct 1998</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c65/">Programming</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c78/">Overload</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c175/">28</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c65-175/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c65+175/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;Notes on Exceptions</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 27 October 1999 18:23:02 +01:00 or Wed, 27 October 1999 18:23:02 +01:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e18" id="d0e18"></a></h2>
</div>
<p>A general remark first: The Harpist seems to assume that at
least the main lines of the implementation are known when the
interface is defined. My personal working domain is different: when
the interface of a general class is defined, little is known about
who will implement it and how it will be implemented. The interface
must stay stable through several different incarnations of the
implementation.</p>
<p>Of course, from this different background follows a different
approach to interface specification, which includes exception
specifications.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e24" id="d0e24"></a>Note 1:
Catching exceptions</h2>
</div>
<p>What is essentially the difference between</p>
<pre class="programlisting">
catch(std::exception &amp;e)
{
  // do some cleanup
  throw;
}
</pre>
<p>and</p>
<pre class="programlisting">
catch(...)
{
  // do some cleanup
  throw;
}
</pre>
<p>One basic rule of exception handling is: &quot;Never handle an
exception you don't know!&quot; So, if you catch a general base class
(such as std::exception), the only thing you should normally do is
some cleanup and rethrow the exception. You can do exactly this
with catch(...).</p>
<p>There is one special case: If you have a parameter, you have an
object that you can copy to store the exception for later use. This
is a technique that is typically required for destructors that get
an exception during stack unwinding. This technique is also
required for smuggling an exception through a layer of C-functions,
like typical GUI frameworks.</p>
<p>But alas, std:exception does not provide a virtual clone()
method, so you can't copy your exception anyway. Indeed the
copy-constructor cannot be used due to object slicing. This is one
of the reasons why every programmer invents her own exception base
class, which in turn should be derived from std::exception.</p>
<p>Now, if you really need to copy an exception you normally don't
know what type it is. This could be at the border between a C- and
a C++-subsystem. So you have good reason to define your own
exception base class as the only one, as the Harpist proposes. But
this is generally the only justification to do so.</p>
<p>But why is it not always useful? Because it's just too much
effort.</p>
<p>You must define your own exception base class, with a virtual
clone(), and then you must duplicate each possible exception of any
library class you use into your own exception hierarchy, and then
you must remap all the original exceptions into your own ones. I
really believe that this turns out as a maintenance nightmare. Not
only do you have all exceptions twice in your system, with
different names and in different class hierarchies, but you also
have to do the duplication again. You'll have to rewrite all your
remapping if you change the library you use, as the new library
will definitely throw different exceptions from the original one.
This type of implementation change happens more often than you
would wish. Also, someone else who uses your classes as a library
would consequently have to do the same, i.e. he has his own
exception hierarchy again.</p>
<p>Now each exception exists three times...</p>
<p>I admit that the concept of an own exception class hierarchy and
consequently providing each function with a respective exception
specification is nearer to the ideal of implementation hiding. But,
implementation hiding was not set up for its own sake, but to
provide better maintainability. In my experience, better
maintainability is achieved by less implementation hiding in the
case of exception specifications.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e51" id="d0e51"></a>Note 2:
Exception Specifications for Destructors and Containers</h2>
</div>
<p>It is true that objects whose destructors have no exception
specifications (they can throw anything) cannot be held by value in
an STL container. However, I would be extremely careful when mixing
exceptions and STL containers with the current standard library
implementations. The main point of my previous letter about
exception specifications and destructors was that you should never
hold base classes by value in containers. You should hold them via
(smart) pointers. Then you have to destroy your objects yourself,
and then you can take care of exceptions from destructors, which
you really should do.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e56" id="d0e56"></a>Note 3: Class
Libraries</h2>
</div>
<p>Perhaps the Harpist and I are not so far apart as it may seem.
He gives very good reasons why the standard library usually
provides no exception specifications. His reasoning applies to most
of the classes I have had to design this way: they are either
templates or framework classes with template methods (&quot;template
methods&quot; in the sense of the &quot;Design Patterns&quot; book by Gamma, Helm,
Johnson and Vlissides). The original Customer class for which the
whole discussion started is just such a class (or at least should
be).</p>
<p>While Ulrich Eisenecker would probably propose to design them as
templates (see
http://home.t-online.de/home/Ulrich.Eisenecker/meta.htm), I prefer
the framework concept with template methods, as this gives better
flexibility without recompiling. Only the final application that
uses your hotel classes should decide, whether the data for the
Customer (and everything else) should be taken from a CORBA server,
an Excel spreadsheet, or a mainframe RDBMS (to mention just a few
possibilities). So, only the designer of the final application
knows of the possible exceptions and has to provide means to handle
them. The designer of the Customer (and any other general hotel
class) can do nothing more than to provide no exception
specifications -- even for the destructors.</p>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
