    <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  :: A Letter from Ken Hagan annotated by The Harpist</title>
        <link>https://members.accu.org/index.php/articles/542</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 #30 - Feb 1999</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/c174/">30</a>
<br />

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

                    -                        <a href="https://members.accu.org/index.php/articles/c65+174/">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;A Letter from Ken Hagan annotated by The Harpist</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="section" lang="en">
<div class="titlepage">
<h2><a name="d0e21" id="d0e21"></a></h2>
</div>
<div class="variablelist">
<dl>
<dt><span class="term">The Harpist:</span></dt>
<dd>
<p><span class="emphasis"><em>I know that some readers do not like
invasive comments on letters but having thought long an hard about
this one I feel that responding to each point as raised makes it
easier to follow. I will write in italic Arial and leave Ken's
original in plain</em></span> Times Roman.</p>
</dd>
<dt><span class="term">Ken Hagan:</span></dt>
<dd>
<p>We actually agree that unwinding is important. Resources must be
released. My point was simply that it doesn't help rollback. I'd be
interested if anyone could present code examples that simplify
rollback.</p>
</dd>
<dt><span class="term">TH:</span></dt>
<dd>
<p><span class="emphasis"><em>Consider:</em></span></p>
<pre class="programlisting">
class Rollable
{
  string first_m;
  string second_m;
public:
  Rollable(
    string const &amp; item1, 
    string const &amp; item2
    )throw (std::exception)
  : first_m(item1),
    second_m(item2)
  {} 
// other members
};
</pre>
<p><span class="emphasis"><em>Now you and I both know that classes
like string are difficult to write so that their copy ctors do not
throw, (more below) so how should I write a copy assignment for
this class. If I leave it to the compiler-generated version it is
possible that <tt class="varname">first_m</tt> has been copied (and
so the original lost) and the effort to copy <tt class=
"varname">second_m</tt> results in an exception. How are we going
to rollback?</em></span></p>
<p><span class="emphasis"><em>If you are unhappy with my use of
string (because of the putative promise that you refer to below)
replace it with any UDT whose construction can
fail.</em></span></p>
<p><span class="emphasis"><em>The first thing I would do is to make
a small design change:</em></span></p>
<pre class="programlisting">
class Rollable
{
  auto_ptr&lt;string&gt; first_m;
  auto_ptr&lt;string&gt; second_m;
public:
  Rollable(
    string const &amp; item1, 
    string const &amp; item2
    ) throw (std::exception)
  :
first_m(auto_ptr&lt;string&gt; (new string(item1))),
second_m(auto_ptr&lt;string&gt; (new string(item2)))
  {} 
// other members
};
Now I can provide:
Rollable &amp; Rollable::operator=
  (Rollable const &amp; rhs)
{
  auto_ptr&lt;string&gt;
    temp1(new string(*(rhs.first_m)));
  auto_ptr&lt;string&gt;
    temp2(new string(*(rhs.second_m)));
// actual copying finished now reseat copies
  first_m = temp1;
  second_m = temp2;
  return *this;
}
</pre>
<p><span class="emphasis"><em>I believe that either that copy
assignment completes satisfactorily or the left-hand object remains
as it was. Maybe we mean different things by commit or rollback
(and certainly there are places where the above mechanism might not
help. Databases that support change in situ spring to mind.) but I
think that the above example follows a simple principle that is
easy to teach and follow: do not modify original data until work
that may raise exceptions has been completed.</em></span></p>
</dd>
<dt><span class="term">KH:</span></dt>
<dd>
<p>We also agree that certain functions must be allowed to throw
since their return values are otherwise constrained. If I may
digress here, Stroustrup says (3rd Ed, 14.4.6.1) that &quot;the standard
library assumes proper (non-exception-throwing) behaviour of copy
constructors&quot;. I think this is forces a reference counted
implementation of <tt class="classname">std::string</tt>. I can't
see how you'd code a non-throwing string copy constructor
otherwise. Indeed, the desirability of non-throwing probably means
that most classes which dynamically allocate their state would be
better off as reference counted implementations. What do you
think?</p>
</dd>
<dt><span class="term">TH:</span></dt>
<dd>
<p><span class="emphasis"><em>I think that you may have
misunderstood Stroustrup. I think he meant that such things as
<tt class="classname">vector</tt> etc. assume that copying will not
throw. In writing that I have just realised why <tt class=
"classname">vector&lt;string&gt;</tt> may be a problem; exactly
because the copy constructor for <tt class="classname">string</tt>
might throw <tt class=
"exceptionname">bad_alloc</tt>.</em></span></p>
<p><span class="emphasis"><em>There is a further problem in that
writing reference counted UDTs is hard if they are to be used in a
multi-threaded environment. I am not saying that it cannot be done,
but most programmers will get it (dangerously)
wrong.</em></span></p>
</dd>
<dt><span class="term">KH:</span></dt>
<dd>
<p>We also agree on static checking. Without it, and especially
with the current confusion over whether the ES is part of the type,
I think we have a hole in the type system. On the related questions
of what ES to apply to template instantiations, I notice that the
compiler could deduce the ES of a function in most cases. In the
absence of the export keyword, template definitions are fully
visible at point of instantiation, and the same logic that checks
the ES could presumably generate one.</p>
</dd>
<dt><span class="term">TH:</span></dt>
<dd>
<p><span class="emphasis"><em>But we are far from any such compiler
and until more programmers want to use exception specifications we
are far from getting one.</em></span></p>
</dd>
<dt><span class="term">KH:</span></dt>
<dd>
<p>I think our disagreements are centred on two issues. First is
our faith in error codes. In the absence of static ES checking, I
don't see that it is easier or harder to ignore exceptions or
return codes. As you say, it is a matter of education. I believe
that ANSI added the (void) cast to C to permit a certain amount of
static checking, so perhaps that counts as a &quot;known or imaginable
analysis tool&quot; for error codes. Also, even in the presence of an
ES, there is still the risk that people will swallow exceptions
rather than dealing with them. Now <span class=
"bold"><b>that's</b></span> something that no analysis tool could
ever spot.</p>
</dd>
<dt><span class="term">TH:</span></dt>
<dd>
<p><span class="emphasis"><em>We may have to agree to differ but
let me take one last cut. Consider the case of the changed
specification for new. If you use <tt class=
"literal">new(nothrow)</tt> and wilfully or otherwise do not check
the return value your program enters undefined behaviour if the
object was not constructed through lack of memory (I am not sure
that that implementation of new doesn't also trap and discard all
exceptions thrown by the constructor. Perhaps one of the standards
experts reading this could comment)</em></span></p>
<p><span class="emphasis"><em>However if you use a version of new
that throws <tt class="exceptionname">bad_alloc</tt> if there is
insufficient memory the program may abort (if the programmer
ignored handling the exception) but it should do so without
damaging other processes and data.</em></span></p>
<p><span class="emphasis"><em>I do not advocate either behaviour
but the latter seems safer to me than the former.</em></span></p>
<p><span class="emphasis"><em>On the subject of swallowing
exceptions, that has to be done explicitly and is clearly a
different order of reprehensible behaviour than simply being
careless.</em></span></p>
</dd>
<dt><span class="term">KH:</span></dt>
<dd>
<p>Our main disagreement however seems to be the environment we
work in. I write COM based software running under Windows. COM
interface methods cannot throw exceptions, since the caller may be
in a different process or a language that doesn't support the
concept. Furthermore, Windows does not tolerate exceptions passing
through OS callbacks, like the window procedures that drive every
visible entity in the GUI. In such an exception-hostile
environment, Francis' <tt class="exceptionname">EndOfProgram</tt>
exception would either disappear without trace or bring the system
down hideously. I have no choice but to place <tt class=
"literal">throw()</tt> on all major interfaces.</p>
</dd>
<dt><span class="term">TH:</span></dt>
<dd>
<p><span class="emphasis"><em>As a matter of interest what does a
call to <tt class="function">exit()</tt> do in such code? Or
raising a signal? Of course there are program contexts where
exceptions must not leak, but how does an error code
help?</em></span></p>
<p><span class="emphasis"><em>As you have just told me that COM
interfaces must not throw I would have thought that any mechanism
that gave a fighting chance that a sane tool provider could produce
a tool to detect breaches would be highly welcome. Obviously in the
context in which you are working you need a <tt class=
"literal">catch(&hellip;)</tt> firewall to deal with potential
exception leakage. But with that firewall in place your COM object
would seem to me to be that much safer.</em></span></p>
</dd>
<dt><span class="term">KH:</span></dt>
<dd>
<p>As an aside, would it be too provocative for me to claim that
the standard library is not a major interface? Even if several
components are written in C++, and sharing the code to whatever
extent the OS allows, they each have to assume the others are
written in another language, so they can't pass library structures
around to each other. It is an implementation detail, hidden from
my clients.</p>
<p>You are a C++ programmer at heart, and you confuse my broader
perspective for C-centricity. 8-)</p>
</dd>
<dt><span class="term">TH:</span></dt>
<dd>
<p><span class="emphasis"><em>That is the one place where we must
disagree. I am a programmer and always try to find the right tool
for the job. C has its place but two of its weaker elements are
signal handling and <tt class="literal">setjmp</tt>/<tt class=
"literal">longjmp</tt>.</em></span></p>
<p><span class="emphasis"><em>Thanks for taking the time to write.
I, and I hope many readers, appreciate it.</em></span></p>
</dd>
</dl>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
