    <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  :: Maintaining Context for Exceptions</title>
        <link>https://members.accu.org/index.php/articles/1232</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 + CVu Journal Vol 15, #4 - Aug 2003</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/c77/">CVu</a>

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

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

                    -                        <a href="https://members.accu.org/index.php/articles/c65+107/">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;Maintaining Context for Exceptions</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 03 August 2003 13:15:59 +01:00 or Sun, 03 August 2003 13:15:59 +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="d0e20" id="d0e20"></a></h2>
</div>
<p>This article is based on a concept presented by Andrei
Alexandrescu at the 2002 ACCU conference [<a href=
"#Alexandrescu">Alexandrescu</a>]. The basic idea Andrei presented
was to store contextual information about the execution stack that
can then be accessed in the event of an exception being thrown. In
this respect, the idea is very much like using a debugger to
display a call stack, except that the information is developer
specified context.</p>
<p>Imagine some highly simplistic code that represents the process
of building a house; the code might be something like:</p>
<pre class="programlisting">
int main() {
  int some_house_id = ... // Get a house identifier
  BuildHouse(some_house_id);
}

void BuildHouse(int house_id) {
  int num_walls = ... // Establish how many walls
                      // are needed.
  for(int i = 0; i &lt;= num_walls; ++i)
  BuildWall(i);
}

void BuildWall(int wall_id) {
  int bricks_required = ... // Establish number of
                            // bricks required
  for(int i = 0; i &lt;= bricks_required; ++i)
  LayBrick(i);
}

void LayBrick(int brick_id) {
  BrickPos bp = GetBrickPosition(brick_id);
  // lay cement, place brick, point join etc.
}

BrickPos GetBrickPosition(int brick_id) {
  BrickPos bp;
  // calculate the brick position.
  if(bp-&gt;Invalid())
    throw InvalidBrickPosException();
}
</pre>
<p>Now if an invalid <i class="parameter"><tt>brick_id</tt></i>
value gets into <tt class="function">GetBrickPosition()</tt>, it
might be reasonable to throw an exception. It is likely that
<tt class="function">LayBrick()</tt> doesn't know why the <i class=
"parameter"><tt>brick_id</tt></i> value is invalid, so the
exception should be caught further down the call stack. In fact,
because the inability to lay a brick means it is difficult to
complete the house satisfactorily, it might be desirable to catch
the exception in <tt class="function">main()</tt> and then to
indicate to the user that the house building process has failed,
and in what circumstances it has failed. So the objective of
providing contextual information with exceptions is to be able to
generate output in a friendly and informative format such as the
following:</p>
<pre class="programlisting">
Error: No brick position identified for brick #brick_id
While getting brick position for brick #brick_id
While laying brick #brick_id
While building wall #wall_id
While building house #house_id
</pre></div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e50" id=
"d0e50"></a>Implementation</h2>
</div>
<p>Ideally, in order to provide the kind of contextual information
described above, the code would be rewritten something like the
following:</p>
<pre class="programlisting">
int main() {
  // Some code to get a house identifier
  try {
    DO_IN_CONTEXT(&quot;While building house&quot;)
    BuildHouse(some_house_id);
  }
  catch(const ContextException &amp; e) {
    std::cout &lt;&lt; e.what() &lt;&lt; std::endl;
  }
}

void BuildHouse(const int house_id) {
  // Code to establish how many walls are needed.
  for(int i = 1; i &lt;= num_walls; ++i) {
    DO_IN_CONTEXT(&quot;While building wall&quot;)
    BuildWall(i);
  }
}

void BuildWall(const int wall_id) {
  // Code to establish number of bricks required
  for(int i = 1; i &lt;= bricks_required; ++i) {
    DO_IN_CONTEXT(&quot;While laying brick&quot;)
    LayBrick(i);
  }
}
// etc.
</pre>
<p>At first appearances, it might be thought that DO_IN_CONTEXT is
a macro concealing a try-catch block, which catches the propagating
exception, adds its contextual information to the exception
message, and then throws a new exception. One of the main arguments
Andrei put forward was that this would be an unnecessary
complication; instead it is preferable that the macro creates an
automatic object whose destructor does the necessary work during
stack unwinding. The idea was to make use of <tt class=
"function">std::uncaught_exception()</tt> in the destructor of this
automatic context object to determine if an exception is
propagating, and therefore whether to add the context information
it contains to the exception.</p>
<p>After the conference, Alisdair Meredith started a thread on
<tt class="literal">comp.lang.c++.moderated</tt> discussing this
concept [<a href="#google1">google1</a>]. The upshot of this
discourse was that the original idea was flawed. Conceptually, the
context object might have looked something like the following:</p>
<pre class="programlisting">
class Context {
public:
  Context(const std::string &amp; context)
    : ex_at_start_(std::uncaught_exception()),
      context_(context) {}
  ~Context() {
    if(std::uncaught_exception() &amp;&amp; !ex_at_start_) {
      // An exception occurred after the constructor
      // executed and before the destructor.
      // Add the context information held to the
      // exception
    }
  }

private:
  bool ex_at_start_;
  std::string context_;
};
</pre>
<p>The problem as discussed on <tt class="literal">c.l.c.m</tt> is
that there is no way to access the propagating exception without
catching it; precisely the situation this object was designed to
avoid. The solution Andrei proposed is to create a context stack
instead of adding the contextual information directly to the
exception. This could either be some kind of static object in a
single-thread application, or thread local storage in a
multi-threaded application.</p>
<p>This mechanism can still make use of an automatic context
object. Instead of storing the context information locally in the
object, the context object's constructor pushes it onto the context
stack. Then the context object pops the last context item from the
stack if its destructor is reached without an exception being
thrown. If an exception is detected in the destructor, the context
stack is left unchanged and the whole of the context information
can be retrieved from the stack at the point where the exception is
handled. A basic implementation of this idea for single-threaded
usage is given below.</p>
<pre class="programlisting">
// rhex_static.hpp

#ifndef RHEX_STATIC_HPP
#define RHEX_STATIC_HPP

#include &lt;string&gt;
#include &lt;deque&gt;
#include &lt;ostream&gt;

#define DO_IN_CONTEXT(str) \
     rhex::Context context(str);
#define DO_IN_SCOPED_CONTEXT(str) \
{ rhex::Context context(str);
#define END_SCOPED_CONTEXT }

// rhex::ExContextHolder

namespace rhex {
class ExContextHolder {
public: // Queries
  static ExContextHolder&amp; Holder();
  std::string LastContext() const;
  void DumpContexts(std::ostream&amp; dest);
  // Modifiers
  void AddContext(const std::string&amp; context);
  void PopLastContext();
private: // Constructors and destructors
  ExContextHolder() {};
  ExContextHolder(const ExContextHolder&amp; rhs);
  ExContextHolder&amp; operator=
                 (const ExContextHolder&amp; rhs);
  // Data members
  std::deque&lt;std::string&gt; contexts_;
}; }

inline
rhex::ExContextHolder&amp;
rhex::ExContextHolder::Holder() {
  static ExContextHolder context_holder;
  return (context_holder);
}

inline
std::string
rhex::ExContextHolder::LastContext() const {
  return (contexts_.empty() ? std::string() :
                                   contexts_.back());
}

inline
void
rhex::ExContextHolder::DumpContexts
                              (std::ostream&amp; dest) {
  while(!contexts_.empty()) {
  dest &lt;&lt; contexts_.back() &lt;&lt; std::endl;
  contexts_.pop_back();
  }
}

inline
void
rhex::ExContextHolder::AddContext
                  (const std::string&amp; context) {
  contexts_.push_back(context);
}

inline
void
rhex::ExContextHolder::PopLastContext() {
  if(!contexts_.empty()) {
    contexts_.pop_back();
  }
}

// rhex::Context

namespace rhex {
class Context {
public: // Constructors and destructors
  explicit Context(const std::string&amp; context);
  ~Context();
private: // Data
  bool exception_present_;
}; }

inline
rhex::Context::Context(const std::string&amp; context)
  : exception_present_(std::uncaught_exception()) {
  if(!exception_present_) {
    rhex::ExContextHolder::Holder().AddContext
                                         (context);
  }
}

inline
rhex::Context::~Context() {
  if(!std::uncaught_exception()
                     &amp;&amp; !exception_present_) {
    rhex::ExContextHolder::Holder().
                                 PopLastContext();
  }
}

#endif // RHEX_STATIC_HPP
</pre>
<p>A <tt class="classname">std::deque</tt> object is preferred as
the stack container because it offers the opportunity to add more
extensive access features to the <tt class=
"classname">ExContextHolder</tt> instance should it be required. A
<tt class="classname">std::stack</tt> container adapter could be
used, but as the default container for this adapter is a <tt class=
"classname">std::deque</tt>, there is no real benefit to doing so
in this case.</p>
<p>In most cases the <tt class="literal">DO_IN_CONTEXT()</tt> macro
is sufficient. In some cases it may be necessary to use the pair of
macros <tt class="literal">DO_IN_SCOPED_CONTEXT()</tt> and
<tt class="literal">END_SCOPED_CONTEXT</tt> (for users of Borland
C++ Builder, a slightly modified version is required due to an
apparent library bug<sup>[<a name="d0e106" href="#ftn.d0e106" id=
"d0e106">1</a>]</sup>. These allow the automatic Context object to
be wrapped in a scope, allowing for nested contexts within a single
function, although in general needing to do this probably indicates
a design flaw as it implies the function is doing too much work. It
allows for constructs such as:</p>
<pre class="programlisting">
void foo() {
  DO_IN_SCOPED_CONTEXT( &quot;While in outer scope&quot; )
    // Some processing which might produce an
    // exception
    DO_IN_SCOPED_CONTEXT( &quot;While in inner scope&quot; )
      // Some more processing which might produce an
      // exception
    END_SCOPED_CONTEXT
  END_SCOPED_CONTEXT
}
</pre>
<p>which might produce output such as:</p>
<pre class="programlisting">
An error occurred
While in inner scope
While in outer scope
</pre></div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e130" id=
"d0e130"></a>Discussion</h2>
</div>
<p>As an interesting aside, the use of the Singleton pattern in
this work was an issue of some considerable concern to the author.
This particular use appears to be an extremely rare case where use
of a singleton can be justified although is not completely
necessary. The code presented here is a work-around required
because of an ineffective language feature [<a href=
"#Sutter2002">Sutter2002</a>], <tt class=
"function">std::uncaught_exception()</tt>, and so is designed to be
much like a language feature; easy to use and non-intrusive.</p>
<p>So why use a Singleton? It might, for example, be preferable to
force the client to create an instance of the context stack at the
beginning of each critical execution path, which is then passed
down the call chain as an additional parameter. But this imposes a
clumsiness and additional overhead for the client programmer. Now
users have to decide when to create context stacks, where to pass
them as parameters and where they should be part of the state of
important classes. Furthermore, context stacks may frequently need
to be passed to functions that don't actually need them, so that
they can be passed on down the call chain. Experience shows that if
this kind of feature is hard to use, developers just won't bother.
For single threaded applications, a Singleton is a simple,
non-intrusive and easy to use solution.</p>
<p>There is a question mark over how useful providing this sort of
context information is. It can be argued that this provides very
little useful information to the end-user, and nothing that a
developer can't get from other sources. In this case, it would be
difficult to argue that the extra overhead involved was worthwhile.
In complex applications, however, where exceptions may be handled a
long way from their source, the extra information is invaluable.
Even with the simple house building example presented earlier, it
is easy to see the benefit. Imagine if this formed part of a larger
system, and houses were not the only type of building that could be
constructed with walls. The bricklaying functions know nothing
about what structure the bricks are for, so the exception either
has to have no information about the type of structure, or coupling
needs to be increased by passing information up the execution
stack. If this formed, for example, part of an automatic batch job
or similar, which would be a preferable message for the operator
upon their return: one saying 'Invalid brick position' or one
saying 'Invalid brick position while building house X'? Picture
also the bug report that your customer files; which message
attached to the report will be easier for the maintainer to get to
grips with?</p>
<p>For many applications, where the additional overhead imposed is
acceptable, the benefits will be considerable, either through
better information, or through a better design because of reduced
coupling between functions, or both. The author's own experience of
using this mechanism on small applications has already shown
benefits. The very first unintentionally generated exception
encountered when using this code immediately showed that some
unexpected recursion was happening, and saved considerable time in
hunting down the source of the problem.</p>
<p>The implementation presented here is very simple, and there is
clearly scope for a much more advanced system for large
applications. One idea that Andrei has suggested [<a href=
"#google2">google2</a>] would be to store a generic base class in
the context stack rather than strings. The base class would specify
some kind of simple output interface, similar, for example, to the
standard printString method in all SmallTalk classes. Developers
would then create more complex, application specific context
classes derived from this base class, which know how to display
their context information.</p>
<p>Where to use such context providers is also an interesting
question. The ideal situation would be to only deploy context
objects along execution paths that could result in an exception,
and where the additional context provided will be useful. In
reality, the complex nature of most software makes it difficult, if
impossible, to identify all possible exceptional execution paths
[<a href="#Sutter2000">Sutter2000</a>]. Instead the emphasis must
be on using context objects at key points; where either significant
runtime information can be added to the context stack, or along the
principal execution paths for the software.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e157" id=
"d0e157"></a>Acknowledgements</h2>
</div>
<p>Thanks to Pete Goodliffe for encouraging me to submit this
article and for providing a helpful informal review of it.</p>
</div>
<div class="bibliography">
<div class="titlepage">
<h2><a name="d0e162" id="d0e162"></a>References</h2>
</div>
<div class="bibliomixed"><a name="Alexandrescu" id=
"Alexandrescu"></a>
<p class="bibliomixed">[Alexandrescu] Alexandrescu, A. 2002. Error
Handling in C++. <span class="citetitle"><i class="citetitle">ACCU
Spring Conference 2002</i></span></p>
</div>
<div class="bibliomixed"><a name="google1" id="google1"></a>
<p class="bibliomixed">[google1] http://groups.google.com: thread
start id: <span class="bibliomisc"><a href=
"http://groups.google.com/groups?q=3CB69B73.9E8BA09C%40uk.renaultf1.com&amp;hl=en&amp;lr=&amp;ie=UTF-8&amp;oe=UTF-8&amp;selm=a99l3b%2418o9e%241%40ID-14036.news.dfncis.de&amp;rnum=1"
target="_top">3CB69B73.9E8BA09C@uk.renaultf1.com</a></span></p>
</div>
<div class="bibliomixed"><a name="Sutter2002" id="Sutter2002"></a>
<p class="bibliomixed">[Sutter2002] Sutter, H. 2002. <span class=
"citetitle"><i class="citetitle">More Exceptional C++: 40 New
Engineering Puzzles, Programming Problems, and Solutions</i></span>
{Item 19}. Addison-Wesley.</p>
</div>
<div class="bibliomixed"><a name="google2" id="google2"></a>
<p class="bibliomixed">[google2] http://groups.google.com: article
id: a99l3b$18o9e$1@ID-14036.news.dfncis.de</p>
</div>
<div class="bibliomixed"><a name="Sutter2000" id="Sutter2000"></a>
<p class="bibliomixed">[Sutter2000] Sutter, H. 2000. <span class=
"citetitle"><i class="citetitle">Exceptional C++: 47 Engineering
Puzzles, Programming Problems, and Solutions</i></span> {Item 18}.
Addison-Wesley.</p>
</div>
</div>
<div class="footnotes"><br>
<hr class="c2" width="100">
<div class="footnote">
<p><sup>[<a name="ftn.d0e106" href="#d0e106" id=
"ftn.d0e106">1</a>]</sup> Rather annoyingly, <tt class=
"function">std::uncaught_exception()</tt> doesn't seem to work
properly with Borland C++ Builder. I found that it always returned
<tt class="literal">false</tt> even when an exception was
propagating. The upshot was that the contexts that should have been
left on the context stack were popped when the various automatic
<tt class="classname">Context</tt> objects were destroyed during
stack unwinding. The slightly ugly solution to this is to push and
pop the context strings manually, avoiding the use of <tt class=
"classname">Context</tt> at all. This requires a pair of macros for
both scoped contexts, but obviates the need for unscoped contexts,
e.g.,</p>
<pre class="programlisting">
#define DO_IN_CONTEXT( str )\
   rhex::ExContextHolder::Holder().AddContext( str );
#define END_CONTEXT\
   rhex::ExContextHolder::Holder().PopLastContext();
</pre></div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
