    <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  :: If Problems Arise</title>
        <link>https://members.accu.org/index.php/journals/1160</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">CVu Journal Vol 14, #2 - Apr 2002 + Programming Topics</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/c77/">CVu</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c115/">142</a>
                    (12)
<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/c115-65/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/journals/c115+65/">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;If Problems Arise</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 03 April 2002 13:15:50 +01:00 or Wed, 03 April 2002 13:15:50 +01:00</p>
<p><strong>Summary:</strong>&nbsp;<p>Every so often I come across the question &quot;should we use return codes or exceptions to report errors.&quot;</p></p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e18" id="d0e18"></a></h2>
</div>
<p>Every so often I come across the question &quot;should we use return
codes or exceptions to report errors.&quot; It is usually the case that
this is asked with the expectation that the answer is either
&quot;return codes&quot; or &quot;exceptions&quot; - even to the extent that after I
explain that this is not so my advice is &quot;simplified&quot; to one of the
above. The correct answer requires a close examination of the
assumptions of the questioner. There are several significant
variations on both return codes and exceptions and there are
further ways to report problems that don't fit under either
banner.</p>
<p>In this article I'm going to provide a checklist of mechanisms
for reporting problems found in the C, C++ and Java languages - it
is left to the interested reader to decide which of the many
options is the one true way sought by my hypothetical questioner.
For the sake of the following discussion suppose that I am
specifying an API function that will normally produce some desired
effect but that will sometimes encounter a problem and need instead
to report on that problem. The following discussion describes a
range of designs that provide that reporting mechanism.</p>
<p>We start with return codes - the principle difference between
these designs is in the way in which the code is communicated to
the calling code.</p>
<div class="variablelist">
<dl>
<dt><span class="term">MainBandReturnCodes:</span></dt>
<dd>
<p>In this case the function return value is always and only a
return code to identify any problem. (If the function also needs to
return a result then I need to consider another option.)</p>
</dd>
<dt><span class="term">InBandReturnCodes:</span></dt>
<dd>
<p>This uses part of the range of possible return values for
results (e.g., positive values) and part of the range for error
codes (e.g. negative values). A typical example of this is
<tt class="function">malloc</tt> from the C standard library (or
the <tt class="literal">nothrow</tt> variant of new in C++).</p>
</dd>
<dt><span class="term">SideBandReturnCodes:</span></dt>
<dd>
<p>In this case the function returns a composite value that
incorporates the both the return code and the result value. The
author of the calling code is then responsible for checking for an
OK return code before using the result.</p>
</dd>
<dt><span class="term">CheckedReturnCodes:</span></dt>
<dd>
<p>An elaboration of SideBandReturnCodes that has been implemented
in C++ is one in which accessing the result without first checking
the status causes either an exception to thrown (or sometimes
<tt class="literal">assert</tt> or <tt class="literal">exit</tt> to
be called).</p>
</dd>
</dl>
</div>
<p>Closely related to return codes are status variables. These have
the potential advantage over return codes that the client code may
call several of these methods before checking the status variable -
although it needs to check the status before using the results.
This can allow a convenient separation of the principle logic from
the error handling. However, this flexibility is not always as
useful as it sounds - unless the API functions are sufficiently
robust (have defined behaviour) following the first failure the
error must still be checked for and handled after each call by the
client code.</p>
<div class="variablelist">
<dl>
<dt><span class="term">GlobalStatusVariable:</span></dt>
<dd>
<p>I guess the most well known example of this is the C standard
library <tt class="varname">errno</tt>. This is a variable that may
be set by many of the library functions if they are unable to
achieve their primary goal. (Note that none of them clear it on
success - clearing <tt class="varname">errno</tt> is left to the
client code.)</p>
</dd>
<dt><span class="term">UserScopedStatusVariable:</span></dt>
<dd>
<p>In this variant the calling code passes a reference (or pointer)
to a status variable into the API function and if a problem occurs
then this variable is updated in much the same way as in
GlobalStatusVariable.</p>
</dd>
<dt><span class="term">ObjectScopedStatusVariable:</span></dt>
<dd>
<p>When the API function is a member-function of an object it is
possible for the status variable to be held on the object instance.
The C++ streams template classes use this approach.</p>
</dd>
</dl>
</div>
<p>Moving on there are mechanisms for altering the program flow -
<tt class="literal">setjmp</tt>/<tt class="literal">longjmp</tt>
and exceptions. These have the big advantages of separating the
normal processing from error handling but in C and Java can make it
difficult to guarantee that paired operations (such as allocating
and freeing resources) are managed correctly.</p>
<div class="variablelist">
<dl>
<dt><span class="term"><tt class="literal">setjmp</tt>/<tt class=
"literal">longjmp</tt>:</span></dt>
<dd>
<p>in C (and theoretically in C++) <tt class="literal">longjmp</tt>
returns the flow of control to a point noted by a previous call to
<tt class="literal">setjmp</tt>. This is rarely appropriate for an
API because of the resource management issues it creates for the
client code.</p>
</dd>
<dt><span class="term">CheckedExceptions:</span></dt>
<dd>
<p>These only exist in Java where they have the effect that the
calling code must make explicit provision for their being thrown
(or the compiler rejects the code). This limits the placement of
error handling code - the exception can only propagate through
methods that make explicit provision for it. These exceptions are
most useful where the method containing the calling code will be
able to handle the error condition -for example when opening a file
fails.</p>
</dd>
<dt><span class="term">UncheckedExceptions:</span></dt>
<dd>
<p>The remaining category of Java exceptions and all C++ exceptions
can propagate without explicit provision. These are best reserved
for serious issues that require any current activity to be aborted
- such as running out of heap space. (I discuss the use of Java
unchecked exceptions in more detail in Overload 48.)</p>
</dd>
</dl>
</div>
<p>All of the above mechanisms have one thing in common - the API
function I'm designing will terminate as a result of the problem.
However, this can be avoided:</p>
<div class="variablelist">
<dl>
<dt><span class="term">CallbackNotification:</span></dt>
<dd>
<p>The calling code may provide a function to be called in the
event of problems (either as a function pointer or by supplying an
implementation of a suitable interface). This provides the
opportunity to continue processing if something can be done about
the problem. Since the possibility of returning from the API call
if the problem can't be resolved must be allowed for it does
complicate the design - and, in practice, this approach seems to be
less useful than it sounds.</p>
</dd>
<dt><span class="term">ResumableExceptions:</span></dt>
<dd>
<p>Although these are not a standard part of any of the languages
they do crop up as extensions. They combine the mechanism of
finding an exception handler up the call stack with the possibility
of the handler &quot;fixing&quot; the problem and allowing processing of the
API call to continue.</p>
</dd>
</dl>
</div>
<p>Any of the above may be appropriate according to the needs of
the particular API function, but this isn't simple advice - and I
haven't come up with a simple decision mechanism that gives good
results.</p>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
