    <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  :: Exceptions &amp; Exception Specifications</title>
        <link>https://members.accu.org/index.php/articles/584</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">Design of applications and programs + 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/c67/">Design</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/c67-175/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c67+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;Exceptions &amp; Exception Specifications</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="d0e14" id="d0e14"></a></h2>
</div>
<p>I have just read the comments from Detlef Vollmann above. He
raises a number of points but starts by making reference to
'dogma'. One person's dogma is someone else's idiom. In my mind,
dogmas are beliefs that are held without reason. So let me try to
present some reasons for using exceptions and exception
specifications in C++.</p>
<p>Because I think it is important I am going to start by assuming
that the reader knows very little of the background to
exceptions.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e20" id="d0e20"></a>Exceptions,
What For</h2>
</div>
<p>When you write a program you become aware of the things that may
go wrong during its execution. For example, your program wants to
open and read a file, but the operating system reports that there
is no such file. How should you handle this?</p>
<p>It rather depends on the context. You may want to allow the user
to try again but you may want to close the program down. If such a
problem occurs fairly near the start of the program, closing down
may be no big deal and code such as:</p>
<pre class="programlisting">
ifstream myfile(&quot;filename&quot;);
if (!myfile)
{
  cout &lt;&lt; &quot;File not found&quot; &lt;&lt; endl;
        return 1;
}
</pre>
<p>in <tt class="function">main()</tt> may be appropriate.
However:</p>
<pre class="programlisting">
ifstream myfile(&quot;filename&quot;);
if (!myfile) 
{
  cout &lt;&lt; &quot;File not found&quot; &lt;&lt; endl;
  exit(1);
}
</pre>
<p>in a member function would almost certainly be an extremely bad
implementation decision. Basically it means that the failure to
open a file will cause any program using that function to
terminate. I will cover the use of <tt class=
"function">exit(1)</tt> a little later.</p>
<p>Most of the time, a failure of this kind (cannot open a file) is
unexpected (not normal behaviour) and will need some special
handling. C provided three mechanisms: handle the problem locally,
return to a previously provided location (via <tt class=
"function">longjmp()</tt> to a location provided by a call to
<tt class="function">setjmp()</tt>) or call <tt class=
"function">exit</tt>. Handling the situation locally includes
returning some form of error code which can, in theory, be used to
provide alternative behaviour in the calling function.</p>
<p>The last of these mechanisms is often appropriate even in C++
but is unavailable in some situations (such as constructors and
destructors). The other two mechanisms are inappropriate to C++
because they will not provide a proper cleanup of the stack.
Actually, even in C, you need to be careful that you do not
<tt class="function">longjmp</tt> in a context that will loose
access to some dynamically assigned resource. Essential cleanup
before program termination can be provided by registering
appropriate functions with <tt class="function">atexit()</tt>.
Actually, you could provide (if you are willing to code the
required tools) something similar to handle cleanups required by
calling <tt class="function">longjmp</tt> but I have never seen
anyone do so.</p>
<p>One of the big themes in C++ is the concept of reuse. This means
that, among other things, code written by one programmer may be
used by others in a wide variety of contexts. Frequently you will
be able to detect a problem in your code but will need to pass it
to the user for solution. A failure in a constructor should not
result in immediate program closure. Before examining how
exceptions can manage this problem, let me cover a couple of other
ideas.</p>
<pre class="programlisting">
class CanFail 
{
  bool failed;
  // other private members
public:
  CanFail(): failed(true)
  {
    // body
    failed = true;
  }
  bool built()
  { return !failed; }
  // other public members
};
</pre>
<p>In other words the class contains a flag that the constructor
sets to failure until the constructor is about to terminate. That
way, after an attempt to construct an object returns you will be
able to check whether it succeeded. A slightly different version of
this technique is in wide use, even in the C++ Standard Library (I
think). The <tt class="function">built()</tt> function is replaced
by:</p>
<pre class="programlisting">
operator void const * ()
{
  return (failed ? 0 : this);
}
</pre>
<p>Do not try to declare an <tt class="methodname">operator
bool()</tt> as it will not always work the way you expect. The
significance of using <tt class="methodname">operator void const
*()</tt> is that there are no available implicit conversions from
<tt class="type">void *</tt> and the <tt class="literal">const</tt>
qualification means that you can do virtually nothing with it
except examine the returned address value. The zero return will
behave as 'false' and any other value will behave as 'true'. The
use of <tt class="varname">this</tt> is just a simple idiomatic way
of providing a non-null address that must be in your program's
actual address space (that is important because a program must not
reference addresses outside that space).</p>
<p>A mechanism like this one is used to provide the check for
successful opening of <tt class="classname">fstream</tt> objects.
It works as long as the resulting object is stable enough for
destruction. But it does depend on the programmer meticulously
checking after every instance of such an object being constructed.
However consider:</p>
<pre class="programlisting">
class CanFailEarly 
{
  Object1 one;
  Object2 two;
public:
  CanFailEarly();
}
</pre>
<p><tt class="classname">CanFailEarly</tt> must construct two data
members. If either fails, what should it do? Well there is no
problem if the two objects are completely independent but suppose
that <tt class="varname">two</tt> depends on <tt class=
"type">one</tt>. If <tt class="varname">one</tt> has failed, how
will you construct <tt class="varname">two</tt>? Does that matter?
Yes, because until you manage to construct two your instance of
<tt class="classname">CanFailEarly</tt> will not be stable and
cannot be safely destroyed.</p>
<p>In other words, we need some way of aborting construction that
will, preferably, cleanup any part-built object and then notify the
program that the object it tried to construct was never completed.
There is no other acceptable general solution to failed
construction other than throwing an exception. The process of
handling an exception has been carefully honed over time so that a
modern C++ compiler should cleanup all the constructed bits before
exiting to your exception handler. If you do not provide such a
handler then your program will unroll until it finds one. If it
cannot find one it will be as if you aborted within the
constructor. The first reaction to that last statement is shock. A
little reflection will show that this is no worse than the best you
could do without exceptions. Certainly any attempt to continue your
program without managing the problem of an incompletely constructed
object will be very dangerous.</p>
<p>Many constructors (if well written) cannot fail. Or to put it
slightly differently, failure of most well written constructors
could only be the result of some event so bizarre that extreme
emergency action is needed. However your program has no way to
recognise this circumstance unless you use exception
specifications. When I write:</p>
<pre class="programlisting">
class CannotFail
{
  &hellip;
public:
  CannotFail() throw();
  &hellip;
};
</pre>
<p>I am not only telling the user that I believe I have written a
robust constructor but I am also telling the compiler to prepare
extreme emergency action should an exception propagate out of that
constructor at run time. The provision of <tt class=
"function">std::unexpected()</tt> (defaults to <tt class=
"function">std::terminate()</tt>) allows the programmer to provide
that emergency action. The default means that unless you provide
your own emergency action the program will terminate. That seems
reasonable to me. In affect, when I add an empty exception
specification I am asserting that this constructor will always
succeed. Unlike the C <tt class="function">assert()</tt> I have the
ability to redefine what will result from an assertion failure.</p>
<p>If my constructors do not have throw specifications, what shall
the user do? S/he knows that potentially any exception can
propagate from this constructor but has no idea what ones. In other
words I must prepare to handle anything. I think that is an
unreasonable design. I should expect you to tell me in what ways
your constructor may fail. It seems to me that your constructor
might fail either because a standard exception has been raised or
because a user supplied one has been. I know I can catch all
standard exceptions with <tt class="literal">catch (exception &amp;
e)</tt>. If users meticulously use exception specifications I will
know what others may occur.</p>
<p>The reason that the Standard Library usually omits an exception
specification is because there are many instances where library
code may call user code (check out the various handlers such as
<tt class="function">std::new_handler()</tt>) where user provided
exceptions might be thrown to be caught by a handler 'on the other
side' of the library and so must pass through the library
functions).</p>
<p>In general, it would be good practice to base all your exception
types on a very limited set of base classes. At worst I think I
should be able to write:</p>
<pre class="programlisting">
class MyClass 
{
  &hellip;
public:
  void func()
    throw(std::exception, 
      MyClassException);
  &hellip;
};  
</pre>
<p>Derived classes should base their exception hierarchies on the
base class base exception.</p>
<p>If I do a correct job users will always know what they are
expected to handle and I will handle everything else. At the moment
this is difficult because so many programmers refuse to provide the
necessary information. Every time I have to call a function without
an exception specification I have to handle everything. For example
(assuming a suitable exception hierarchy for <tt class=
"classname">MyClassException</tt>):</p>
<pre class="programlisting">
void MyClass::func()
   throw (exception, MyClassException)
{
  try {
    // body of fucntion
  }
  catch (exception &amp; e)
  { throw; }
  catch(&hellip;) 
  { throw MyClassException::unknown; }
}
</pre>
<p>That is the best I can do if any function called in the body
lacks an exception specification.</p>
<p>I think that exception specifications are, to C++ programmers,
what <tt class="literal">const</tt> is to C programmers. C++
programmers (good ones at least) know that using <tt class=
"literal">const</tt> and ensuring <tt class="literal">const</tt>
correctness provides better code with fewer unexpected results. The
next generation of C++ programmers may realise that exception
specification correctness is a vital element to writing good robust
code.</p>
<p>Overly generalised code is unhelpful. Learning to write and use
exception specifications takes time but many of the perceived
problems go away when you look beyond novice attempts. The problem
with exception specifications in over-riders for virtual functions
largely goes away once you start providing base class exception
hierarchies. If you do not like cluttering up your class
definitions then place the base for your class hierarchy in the
namespace you are building your class hierarchy in. If you do not
automatically encapsulate your class hierarchies in namespaces it
is time to ask why not.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e172" id="d0e172"></a>Exception
Specifications and Destructors</h2>
</div>
<p>Detlef seems to have a problem with destructors with empty
exception specifiers. I have a problem with non-empty ones. Let me
explain why.</p>
<p>You cannot have a container of any type whose destructor can
throw.</p>
<p>That may seem a strong statement but I cannot see any way round.
Consider when the container is being destroyed. Each element must
itself be destroyed. This in turn may result in calling destructors
for all the subobjects (base class objects or member objects) of
each element. If any exception propagates out of the destructor of
an element what happens next?</p>
<p>You will have a part destroyed container. You will not be able
to roll back to the state before you started destruction and you
will not be able to complete the process. Any exception propagating
out of an element of a container destabilises the container. I know
of no way round this. In extremis, almost every other action that
you might take on a container can be provided with commit or
rollback semantics (though you might think the performance cost
exorbitant) but destruction cannot work that way. Once Humpty
Dumpty has his great fall all the programmers in the World cannot
put him back together again.</p>
<p>Note that I did not say the destructors must always have empty
exception specifications. What I did say was that if you elected
not to have an empty exception specification you should be required
to document your decision.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e185" id=
"d0e185"></a>Conclusion</h2>
</div>
<p>Now that we have compilers that at least tolerate exception
specifications we should learn to use them. This does not mean
slapping an exception specifier on every function. It means that we
must understand what they are for and how we should use them. It is
my contention that by the time you get to the public interface for
an application level class all functions should have exception
specifications. In general every effort should be made to make
these empty. The application level programmer should have to do
very little catching of exceptions. To a large extent <tt class=
"function">main</tt> should look like this:</p>
<pre class="programlisting">
int main()
{
  try
  {
    // your code
  }
  catch (ProgramEnd &amp; pe)
  {
    pe.report();
    return pe.errorlevel();
  }
  catch (&hellip;)
  {
  cout
    &lt;&lt; &quot;Unexpected Program Termination&quot; 
    &lt;&lt; endl;
    return (1);
  };
  return 0;
}
</pre>
<p>This means that functions called in the <tt class=
"literal">try</tt> block can propagate a <tt class=
"exceptionname">ProgramEnd</tt> exception to cause orderly
termination of the program with a full cleanup of the stack. Just
to be safe and ensure a cleanup if possible, we catch all other
exceptions, print an error message and terminate with an error
return to <tt class="function">exit()</tt>. Finally the program can
terminate normally through a normal return to <tt class=
"function">main()</tt>. with a <tt class="literal">return 0</tt> to
<tt class="function">exit()</tt>.</p>
<p>Of course it is difficult to achieve this level of clean
programming while we have to handle so much dirty code but that is
no reason for avoiding exceptions and exception specifiers.</p>
<p>Unlike Detlef, I think the problem lies with those who want to
live without exceptions. Exceptions and exception specifiers are
certainly useful for constructors (remember that if the constructor
of a subobject throws an exception any already constructed
subobjects will be destroyed). And it is close to essential for
destructors to handle exceptions internally. I know we tend to hate
new things, particularly when they cause us to change the habits of
a lifetime.</p>
<p>To my way of thinking, a designer who fails to provide exception
specifications is failing in his/her duty to users of the code. If
you cannot promise me that your code will not propagate exceptions
I have a right to ask you what exceptions it might propagate. Of
course this is extra work, but writing reusable code is extra work,
you do not get the benefits at zero cost. The earlier exceptions
are specified the less the burden on users of the code. You may not
like the constraint that exception specifications in a base class
place on your derived class, but hey, you want to use my work and I
should have the right to place constraints on how you use it. It's
my ball so you play by my rules. In practical terms, I am (possibly
legally) responsible for my code and should be able to limit the
ways in which you can abuse it.</p>
<p>Good programmers qualify globals as <tt class=
"literal">static</tt> (or better, place them in an anonymous
namespace) until they are certain that external linkage is
necessary, they qualify member functions as <tt class=
"literal">const</tt> until they know they must mutate the object
state, they qualify constructors as <tt class=
"literal">explicit</tt> until they are certain that implicit use is
safe, they qualify functions with <tt class="function">throw()</tt>
until they know different. In each case we have to make explicit
something that should have been the default behaviour but we live
with our pasts. If we could start from scratch we might do many
things differently including making no exception propagated the
norm for functions.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e235" id="d0e235"></a>An Aside</h2>
</div>
<p>By the way, with all of its much heralded garbage collection,
can someone tell me what guarantees Java provides with its
exceptions. If the construction of an object that handles some
resource other than memory fails do I know that the resource is
released? Perhaps someone could write an article about exceptions,
constructors and destruction in Java.</p>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
