    <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  :: Exception Handling Alternatives</title>
        <link>https://members.accu.org/index.php/journals/546</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>


        <h2>Journal Articles</h2>


<div class="xar-mod-head"><span class="xar-mod-title">Overload Journal #30 - Feb 1999 + Programming Topics + Design of applications and programs</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/journals/">All</a>

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

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

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c174/">30</a>
                    (11)
<br />

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

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

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

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

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

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c67/">Design</a>
                    (236)
<br />

                                            <a href="https://members.accu.org/index.php/journals/c174-65-67/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/journals/c174+65+67/">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;Exception Handling Alternatives</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 26 February 1999 16:50:51 +00:00 or Fri, 26 February 1999 16:50:51 +00: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>After having read a lot of C++ exception discussions in the
previous issues of Overload, I will introduce some alternate
methods of handling exceptional events.</p>
<p>Exceptional events occur. But, since they are exceptional, they
occur very rarely. This is exactly the problem with them. In
programming courses you learn to always handle any possible event.
But, in practice, most programmers just ignore them. If you look at
this problem in detail, you see that these events are not ignored
where they actually occur, but at some higher level. For example, a
&quot;Disk Full&quot; error is detected in the output routine (perhaps printf
or operator&lt;&lt;) and is flagged, but the calling routine
ignores the error. This is exactly why the exception mechanism was
introduced into C++. You just cannot ignore an exceptional event
after it has been detected and thrown. But, as has been discussed
in previous articles, exceptions have other problems. As I wrote
earlier, the exception mechanism of C++ is necessary as a
substitution for the C longjmp mechanism. But, I still think this
standard mechanism is not good enough for working with exceptional
events. So, I will suggest some alternative mechanisms that can
handle at least some of the events with greater ease.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e24" id="d0e24"></a>Example</h2>
</div>
<p>As a running example, I will use a typical framework system that
implements the Builder pattern from the GoF (&quot;Gang of Four&quot;,
nickname for the authors) book [<a href="#GoF">GoF</a>].</p>
<p>The application is a CASE graphical editor, and the framework
provides classes to read in a design diagram from a file in a
specific format (e.g. CDIF) and to build an internal representation
of the diagram. The framework provides an abstract base class
Input, which has a pure virtual function readChar() that must be
implemented by the application developer in a derived class. This
function might encounter a problem on reading a character, and the
framework designer must decide how this problem shall be reported
to the framework and the application. Of course, end-of-input is
not an exceptional event and therefore must be handled by normal
processing, probably by a special read character returned.</p>
<p>(BTW, this design is a non-template implementation of the
Iterator pattern. But there are not many differences if you decide
to use a templated approach).</p>
<pre class="programlisting">
class Input
{
public:
// The derived class might open a file in 
// the constructor, or a network 
// connection, ...
  Input();  
// No (empty) exception specification 
// here, because you never know what the 
// destructor of the derived class might 
// do here.  But I will not discuss this 
// issue further in this article.
  virtual ~Input();  

// About the exact declaration see the 
// text.
  virtual SomeReturn readChar() = 0; 
};
</pre>
<p>Another part of the framework provides the classes to build the
internal representation. An abstract base class DiagramBuilder is
provided that declares the virtual functions for building the parts
of the diagram. The application must subclass DiagramBuilder and
implement these functions.</p>
<p>class DiagramBuilder { public: // ... virtual void
buildEntity(EntityInfo&amp;) = 0; virtual void
buildRelation(RelationInfo&amp;) = 0; // ... };</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e42" id="d0e42"></a>Return
Codes</h2>
</div>
<p>The first alternative to standard C++ exceptions I will present
are return codes. This was extensively discussed by Ken Hagan and
The Harpist in the last issue.</p>
<p>If you must solve the example problem with return codes, you
might find a special character to return in case of a problem, but
a better solution would be to declare readChar like this:</p>
<pre class="programlisting">
ReturnStatus readChar(char &amp;c);
</pre>
<p>Here, readChar returns the read character in c and reports a
problem as a return value of type ReturnStatus, which might be a
simple enum or a complete object containing all interesting
information for the problem. In the latter case, the return value
should be returned by reference, as the returned object might be of
some class derived from ReturnStatus.</p>
<p>For DiagramBuilder, the functions must be declared to return a
status (probably again a derivation of ReturnStatus by
reference).</p>
<p>The problem with this approach is that the framework does not
know what to do with the error, and must now unwind the stack
manually until it can return the error status to the application.
This is the classic problem of error handling in frameworks, where
error detection and error handling are absolutely not local.</p>
<p>But, where you could perhaps use return codes quite well is
inside the framework. In this aspect, I perhaps agree with Ken
Hagan. Where error handling is local, and you are in control of
both the source of the exceptional event and the handling of it
(and, of course, your source is not inside of a constructor or an
overloaded operator), then return codes are probably the best
technique for handling exceptional events. But, you must be sure
that you are really in complete control of both the source and
handling points, i.e. neither of them is inside a virtual function
that may be overwritten in a derived class. So, on second thoughts,
for frameworks where most classes may be specialized by the user of
the framework, return codes are not a good idea for handling
exceptional events.</p>
<p>The main problems with the return codes approach are:</p>
<div class="orderedlist">
<ol type="1">
<li>
<p>They are unusable for constructors, destructors and overloaded
operators.</p>
</li>
<li>
<p>They are difficult to propagate to the place where the event can
be handled consistently for the whole application. In our example
the problem occurs inside an application function, and it must be
handled by the application, but the framework, which doesn't know
what kind of problems might occur, must itself propagate the
error.</p>
</li>
<li>
<p>Programmers ignore them.</p>
</li>
</ol>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e71" id="d0e71"></a>Deferred Error
Handling</h2>
</div>
<p>Another possibility for handling an error is &quot;deferred error
handling&quot; (as I have seen first proposed by James Kanze in
[<a href="#Kanze1">Kanze1</a>]). This technique is used by the
&quot;classic&quot; iostream-library. Here, an object internal error flag is
set, if a problem occurs. This flag can be checked by clients using
a special member function (e.g. overloaded !-operator). All member
functions of the class check the flag and act respectively
(probably by doing nothing).</p>
<p>In our example, readChar() could set an internal flag, and
return end-of-input. This would cause the framework either to
report an error (&quot;premature end of file&quot;) or just to finish with an
incomplete representation. In both cases the application has to
check for the flagged error and must handle it appropriately. To be
able to identify the error correctly, the flag must not only be a
binary value but probably a full exception object.</p>
<p>If a function of DiagramBuilder detects a problem, again a flag
is set, but the function will return normally. So, the framework
will not notice that an exceptional event occurred and will just
continue to create the diagram. But, all subsequent calls to one of
the member functions of DiagramBuilder will see the set error flag
and just do nothing. After the framework has completed the diagram,
it will return to the application, which will check the error flag
and handle it.</p>
<p>In all cases, the framework has nothing to do with the handling
of any of the exceptional events; it won't even notice when one has
occurred.</p>
<p>One problem arises with the location of the flag: the objects
for Input or DiagramBuilder may no longer exist when the diagram is
completed. So, the flag must be in an object that still exists
after the completion of the building process. For DiagramBuilder,
this would obviously be the created diagram representation, but for
Input it is difficult to find a good location, as the input object
itself is created by the framework and only visible from there.</p>
<p>The advantage of deferred error handling is the invisibility to
the non-interested parts of the application. In our framework
example the errors are completely transparent.</p>
<p>A disadvantage of deferred error handling is the long time,
which might pass between the detection and the handling of an
error. The application must be carefully written to ensure that
endless loops are not created. For example, if the framework needs
some information from the diagram representation to complete its
task, this information must be given.</p>
<p>Deferred error handling is best used for classes whose
responsibilities are non-critical, e.g. output for purely
informational logging. In this case the application is run until
successful completion, and than the error flag is checked and some
notification message might be issued.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e93" id="d0e93"></a>Error
Stack</h2>
</div>
<p>David Vandevoorde proposed in [<a href=
"#Vandevoorde">Vandevoorde</a>] the usage of a global error stack.
This is effectively a variant of the standard C++ exception
mechanism, but without hidden control flows and everything
(including stack unwinding) must be done explicitly. When a problem
is detected, instead of throwing an exception, or to flag it
internally as in the deferred error handling mechanism, it is
pushed down onto a global stack (in a multi-threaded environment,
you have to decide whether you want one stack per thread or one per
process). Then, each critical section (or the member functions of
critical objects) must check the error stack to ensure the
consistency of the manipulated object. If a problem is pending
(there is some exception on the stack), nothing is done. So far
this is similar to the deferred error handling mechanism. But, any
function can decide to handle the exception and pop it from the
stack. This is similar to the standard exception handling
mechanism. Where you would have a catch clause, you now just check
for an entry on the stack.</p>
<p>Both our example framework and application can make use of this
mechanism. If only the application were to use an error stack then
the readChar and application DiagramBuilder functions would put an
exception on the stack when a problem occurs, and they would check
for an exception on entry and act as in the deferred error handling
mechanism. When the framework eventually returns control to the
application, the application would check for any pending exceptions
on the stack.</p>
<p>If the framework also implemented the error stack mechanism,
then the application would have to use the same stack, and the
readChar and the buildX functions would have to push their
exceptions on to this same stack. The framework then typically
checks the stack for exceptions directly after each call to one of
the functions provided by the application. If an exception is
pending it has to unwind the stack manually (i.e. return from all
its functions) and return control to the application, which then
has to handle the exception. In this case, the framework reports
its own problems to the application also via the exception
stack.</p>
<p>The error stack mechanism is essentially the same mechanism as
standard C++ exception handling. You typically cannot do anything
useful as long as there are unhandled exceptions. Your only actions
when you have hanging exceptions are in most cases clean-up actions
and manual stack unwinding (returning from the function). But, you
have no hidden stack unwinding without your control, and you can
have more than one exception at the same time on the stack, so you
don't have artificial (i.e. coming from the C++ environment and not
from the problem domain) problems with exceptional events in
destructors.</p>
<p>Since the error stack is not a standard mechanism of C++, you
cannot assume that any 3rd party code will use this technique. And,
of course, you lose the capabilities of the built-in mechanisms.
So, if the constructor of one element of an array fails, but this
failure is only reported by an exception on the stack, all other
elements are constructed as well, and you have to destruct them
again manually. But, this is more a problem of efficiency and not
of the principle. Another problem is to find the best match for the
error handling action. This is a typical multiple dispatch problem,
as you have two criteria (the place of the handling and the type of
the exception) for which you must find the best matching action.
With C++ exceptions, the C++ environment helps you to define this
match. With an error stack, you have to do it yourself, and you
must use some technique as described in the GoF Visitor pattern (or
one of its many variants). Yet another problem is the possibility
of ignoring the exception and performing unsafe actions on
inconsistent objects. But, this can (at least partially) be avoided
by the mixing with the deferred error handling mechanism.</p>
<p>An error stack is effectively a mix of all the other techniques.
It has elements of C++ exceptions, return codes and deferred error
handling. It also has its own mix of pros and cons: No hidden
control flow, but it can be ignored, no problems with exceptions
from destructors, but no compiler supported best match of handling
action. Also, as it is a rather unknown technique I would not use
it as the standard exception handling mechanism for the framework.
But, it might be a good choice for the application part of our
example (i.e. the transfer of errors from readChar and buildX
through the framework back to the application.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e111" id="d0e111"></a>Safe
Termination</h2>
</div>
<p>Often, a safe termination of the program is the best approach.
James Kanze provided in [<a href="#Kanze2">Kanze2</a>] the
following categorization of applications:</p>
<div class="orderedlist">
<ol type="1">
<li>
<p>Critical: if this fails, a backup system will step in</p>
</li>
<li>
<p>Small standalone programs like compilers: if they crash they
will just be restarted by the user</p>
</li>
<li>
<p>Other; e.g. this might be a server in a client/server
application: it should not crash, but there is no special fail-over
environment.</p>
</li>
</ol>
</div>
<p>While on first consideration a lot of applications seem to
belong to the third category, on closer inspection most fall into
categories (1) or (2). For example, a server might spawn a new
process for each client. If that process crashed then either the
client must be restarted (then the server belongs to category (2)
because it only serves this client), or the server is must be
restarted automatically (by a process monitor) and the client
connection must be automatically recreated; in this case the server
belongs to category (1), even if there is no special hardware
backup system.</p>
<p>You might also terminate and produce a post mortem dump to aid
debugging of the problem. This is the best approach for dealing
with programming errors. Unfulfilled pre-conditions, such as &quot;index
out of range&quot; or &quot;pop from empty stack&quot;, must generally be treated
as programming errors - and what can you usefully do with a
programming error other than to terminate the program and
debug?</p>
<p>Our example probably falls into category (2); so, termination
might be a useful solution. The readChar and the functions of
DiagramBuilder would just terminate the program on detection of a
problem. One way to terminate the problem is the EndProgram
exception, which enables all functions on the stack to correctly
clean up all held resources. For applications with resources that
are not freed by the operating system this is one way to avoid
resource leaks. But, this again introduces C++ exceptions into the
system. A solution to avoid C++ exceptions is to register all
external resources at a global place and to free them from the
termination routine. Other duties of the termination routine are to
inform other threads and/or processes about the termination. When
the process is restarted by a process monitor, this monitor could
be informed of open client connections etc. so that the restarted
process could seamlessly fill in the place of the terminated
one.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e135" id="d0e135"></a>Review So
Far</h2>
</div>
<p>For the design of the example framework error handling, there
are two different decisions to make:</p>
<div class="orderedlist">
<ol type="1">
<li>
<p>How to report errors of the framework to the application.</p>
</li>
<li>
<p>How to enable the application classes derived from the framework
classes to report problems.</p>
</li>
</ol>
</div>
<p>In both cases, the application has to handle the problem, and
not the framework. All the above mechanisms and standard C++
exception handling could be used as design solution. And, as we
have seen, it might make sense to use different mechanisms for (1)
and (2). And even for readChar and buildX, which are both of type
(2), it could be useful to use different mechanisms. So, the
designer of a truly generic framework should not setup a fixed
error handling mechanism, but leave these decisions open to the
application designer. How to do this easily, and how to solve other
problems of all the above mechanisms as well, I will show you in
the next Overload issue.</p>
</div>
<div class="bibliography">
<div class="titlepage">
<h2><a name="d0e149" id="d0e149"></a>References</h2>
</div>
<div class="bibliomixed"><a name="GoF" id="GoF"></a>
<p class="bibliomixed">[GoF] Erich Gamma, Richard Helm, Ralph
Johnson, John Vlissides: Design Patterns, Addison-Wesley 1994, ISBN
0-201-63361-2</p>
</div>
<div class="bibliomixed"><a name="comp.lang" id="comp.lang"></a>
<p class="bibliomixed">[comp.lang] In spring 96, there was a very
interesting Usenet discussion &quot;Safe use of exceptions: possible?
worth the trouble?&quot; in comp.lang.c++.moderated. The whole
discussion is still very recommended to read. The thread started
with Message-ID &lt;<span class=
"bibliomisc">4ljuch$aap@netlab.cs.rpi.edu</span>&gt;</p>
</div>
<div class="bibliomixed"><a name="Kanze1" id="Kanze1"></a>
<p class="bibliomixed">[Kanze1] James Kanze, above discussion,
Message-ID &lt;<span class=
"bibliomisc">4ncc5ua@netlab.cs.rpi.edu</span>&gt;</p>
</div>
<div class="bibliomixed"><a name="Vandevoorde" id=
"Vandevoorde"></a>
<p class="bibliomixed">[Vandevoorde] David Vandevoorde, above
discussion, Message-IDs &lt;<span class=
"bibliomisc">4mlfh0$g@netlab.cs.rpi.edu</span> &gt; and &lt;
<span class="bibliomisc">4mr6fr$qqv@netlab.cs.rpi.edu</span>
&gt;</p>
</div>
<div class="bibliomixed"><a name="Kanze2" id="Kanze2"></a>
<p class="bibliomixed">[Kanze2] James Kanze, above discussion,
Message-ID &lt;<span class=
"bibliomisc">4n07r5$mkg@netlab.cs.rpi.edu</span> &gt;</p>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
