    <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  :: Exceptional Java</title>
        <link>https://members.accu.org/index.php/journals/406</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 #48 - 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/c78/">Overload</a>

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

                    -                        <a href="https://members.accu.org/index.php/journals/c197+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;Exceptional Java</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 26 April 2002 17:46:09 +01:00 or Fri, 26 April 2002 17:46:09 +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="d0e18" id="d0e18"></a></h2>
</div>
<p>It must be approaching twenty years since I visited the computer
science department of the local university and on one of the notice
board spotted a chart for the next decade of development in the
industry. The feature of this that I remember was that every odd
year predicted &quot;the end of Fortran&quot; and every even year &quot;the end of
Cobol&quot;. As the author (and I) expected, these languages are still
thriving far beyond the end of that chart.</p>
<p>While to work in the software industry is to be exposed to
constant change there is much that is constant in spite of that
change. While we are regularly exposed to new tools (technologies,
languages, techniques, etc) our existing knowledge and tools remain
stubbornly useful. This is especially true when we distinguish the
fundamental ideas from work-arounds for a specific tool.</p>
<p>For some years I've been very happy with the use of exceptions
in C++ programs [<a href="#Griffiths1999">Griffiths1999</a>].
Recently I accepted a position at a company working primarily in
Java, and consequently had to address the problems being
encountered by developers using this language.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e29" id="d0e29"></a>Received
wisdom</h2>
</div>
<p>Before I go on to describe the problems encountered by my new
colleagues, let me revisit the &quot;received wisdom&quot; of the Java
development community. This is represented clearly by the following
quote from &quot;Thinking in Java&quot; [<a href="#Eckel2000">Eckel2000</a>]
(similar views are expressed by other sources):</p>
<div class="blockquote">
<blockquote class="blockquote">
<p>Keep in mind that you can only ignore <tt class=
"classname">RuntimeException</tt>s in your coding, since all other
handling is carefully enforced by the compiler. The reasoning is
that a <tt class="classname">RuntimeException</tt> represents a
programming error:</p>
<p>An error you cannot catch (receiving a null reference handed to
your method by a client programmer, for example).</p>
<p>An error that you, as a programmer, should have checked for in
your code (such as <tt class=
"classname">ArrayIndexOutOfBoundsException</tt> where you should
have paid attention to the size of the array).</p>
</blockquote>
</div>
<p>It is sensible to use <tt class=
"classname">RuntimeException</tt>s to report programming errors -
the availability of a call-stack aids reporting them meaningfully.
The fact that they can be handled far up the call stack makes
implementing an application-wide policy for handling them easier
than trying to do so at every point an error is detected. (Examples
of policies from different types of application domain are: to
abort the current operation, to restart the subsystem, or to
terminate the process.)</p>
<p>However, the &quot;received wisdom&quot; very clearly directs a developer
towards using ordinary, checked exceptions (i.e. those not derived
from <tt class="classname">RuntimeException</tt>) in the design of
an application. The use of <tt class=
"classname">RuntimeException</tt>s is discouraged: &quot;programming
errors&quot; do occur; but - beyond having a policy for dealing with
them when detected - we should not be creating a design to cater
for them! (Trying to cater for bugs only leads to hard to test code
that is, itself, a breeding ground for bugs.)</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e66" id="d0e66"></a>The difference
between theory and practice&hellip;</h2>
</div>
<p>The developers that I joined had attempted to apply this
guidance and run into a number of problems. However, because of
pressure to &quot;just write the code&quot; no attempt had been made to
formulate a workable design policy. Letting developers struggle on
independently can waste a lot of time over the course of a project
- so I called a meeting of the programmers on the team I was
working with to discuss the problems they were having with
exceptions.</p>
<p>We'll be examining more of the problems they described in the
rest of this article - this section deals only with those that bear
directly on the above theory (that checked exceptions should be
used for everything that isn't a programming error). Before
proceeding, I want to make it clear that this development group
isn't the only one to experience these problems. They are in good
company - as I confirmed at the ACCU Spring conference last year.
It seems that this theory doesn't work in practice.</p>
<p>The relevant problems described fall into three categories.</p>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e75" id="d0e75"></a>Breaking
encapsulation</h3>
</div>
<p>Consider the example of a factory method that is responsible for
creating objects. From the point of view of the client code there
is no obvious reason why it should fail - so the interface doesn't
have a throws clause. From the point of view of an implementation
that retrieves objects from a database it is necessary to handle
<tt class="classname">SQLException</tt>s. For the sake of this
discussion let us assume that these reflect something catastrophic
- like connectivity to the database being lost.</p>
<p>The <tt class="classname">SQLException</tt>s we are considering
are not the result of programming errors. Accordingly, we are
exhorted not to propagate them as unchecked exceptions. On the
other hand we cannot handle them locally (except by the unhelpful
expedient of returning a null reference). This leaves two options:
either adding a throws clause to allow the factory method to
propagate an <tt class="classname">SQLException</tt> or throwing
our own exception (normally one that &quot;wraps&quot; the original
exception).</p>
<p>Either approach places a burden on the client code - which will
generally be in no better position to handle the error than the
factory method itself. (Clearly, this is an iterative argument;
but, somewhere far up the call stack there will be some code that
manages the error.)</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e93" id="d0e93"></a>Loss of
information</h3>
</div>
<p>The strategy of &quot;wrapping&quot; exceptions prior to propagating them,
alluded to in the last section, has the unfortunate side effect of
making it difficult to detect the distinction between different
problems programmatically. Essentially, one ends up with the
situation that all that the programmer can be sure of is that
&quot;something went horribly wrong&quot;. Admittedly, that is often enough
but it occasionally limits options. For example, how can one decide
if is it worth retrying the operation that failed?</p>
<p>The alternative strategy (of allowing the original exceptions to
propagate) can lead to a similar loss of information. Writing
multiple, nearly identical, catch blocks is tedious and a potential
source of the familiar maintenance issues caused by &quot;cut and
paste&quot;. Sooner or later, someone just writes &quot;<tt class=
"literal">catch (Exceptions e)</tt>&quot; - forgetting the (usually
unintended) side effect that this also catches <tt class=
"classname">RuntimeException</tt>s.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e106" id="d0e106"></a>Information
overload</h3>
</div>
<p>Depending upon whether exceptions are allowed to propagate or
are &quot;wrapped&quot; two things can happen. Either, an increasing and
incoherent set of exceptions begin to appear in the throws clauses
of methods dependent on others - until developers get fed up with
this insanity and introduce &quot;<tt class="literal">throws
Exception</tt>&quot;. Or, nearly every method has code to catch
exceptions propagated from the methods it depends upon, &quot;wrap&quot; them
in a new exception that makes sense within its interface and throw
the new exception.</p>
<p>The theory sounds bad enough but, in practice, there is another
thing that happens (although no developer admits to doing this
intentionally). That is to consume an inconvenient exception by
catching and ignoring it.</p>
<p>If these problems that occur in practice are not enough to
justify considering alternatives there is a further difficulty: you
may be coding to a function signature that doesn't allow you to
throw any checked exceptions. This can happen when you are
implementing an interface that you don't control (for example
<tt class="classname">Comparator</tt>).</p>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e121" id="d0e121"></a>Fundamental
ideas</h2>
</div>
<p>In C++ I use exceptions to report problems that it is
unreasonable to expect the immediate client code to deal with. In
particular, the example of the factory function described above
seems to satisfy these criteria: The part of the system that knows
how to deal with loss of the database connection is likely to be
many layers away from a factory function that retrieves objects
from a database.</p>
<p>The problems related in the last section suggest that this
approach isn't working - so either the approach is wrong or it is
being implemented incorrectly. There are many differences between
C++ and Java but there are also lots of similarities between them.
Specifically, there are enough similarities in the exception
handling mechanisms that I'd expect to use Java Exceptions for the
same things that I'd use C++ exceptions for.</p>
<p>What the problems identified above illustrate is the ways in
which Java checked exceptions are not like C++ exceptions.
Declaring a checked exception places an obligation on the caller of
a method to do something explicit with the exception - and that is
precisely what isn't desired. What is desired is to transfer
program flow in an orderly manner to some point far up the call
stack.</p>
<p>There is something in Java that looks far more like my familiar
C++ exceptions than Java's checked exceptions: Java's unchecked
exceptions. To me they looked like the answer - it doesn't take
much thought to conclude that approaching the above scenario by
wrapping the <tt class="classname">SQLException</tt>s in an
unchecked exception causes none of the above problems.</p>
<p>I outlined this approach at the meeting, and there was general
consensus that it made sense, but a number of concerns were
expressed: mostly regarding the choice that exists between the two
exception-handling mechanisms. One of the senior team members
agreed to use his notes from the meeting to draft a guideline
&quot;exceptions strategy&quot; paper. This would be reviewed at a subsequent
meeting when everyone had had an opportunity to think about it. (It
is a good idea to review such solutions a few days later - it is
very easy to be seduced by an attractive idea and overlook a killer
issue during a brainstorming session.) The current version of this
paper is included below.</p>
<p>Later on that day I was approached by one of the more thoughtful
team member who was concerned that while he couldn't see anything
wrong with what I was suggesting he couldn't find any books - or
reference material on the internet - that agreed with it. I'm
always pleased to be approached like this as I can be wrong, and
much of the value of such meetings is lost if people don't think
and research for themselves. In this case, I view this as a
reflection of the immaturity of the Java community - the hype
surrounding the language often gets in the way of recognising an
issue and finding a solution. It took the C++ experts from 1990
(when they were introduced as &quot;experimental&quot; in the ARM [<a href=
"#Stroustrup1990">Stroustrup1990</a>]) to 1997 (when the C++
standard library was revised to specify its behaviour in the
presence of exceptions) to get a consensus on how to use
exceptions.</p>
<p>I knew from my experience with C++ that there are ways to write
code that works in the absence of the compiler prompting the
developer to deal with exceptions. Indeed, I knew the fundamental
ideas used in managing exceptions in C++ could also be applied to
Java: I presented a translation of them at the ACCU conference 2001
[<a href="#Griffiths2001">Griffiths2001</a>]. (Anyway, it isn't the
first time I've disagreed with authority - and it surely won't be
the last!)</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e147" id="d0e147"></a>One more
problem</h2>
</div>
<p>There was one more significant problem that had been observed in
a number of existing systems. In these, it had been found to be
difficult to handle the errors reported via exceptions effectively.
This was believed to be a result of every type of failure reported
being reported using the same exception type - albeit with
different message text. (If this sounds to you like the <tt class=
"literal">java.sql</tt> package and ubiquitous <tt class=
"classname">SQLException</tt> then you won't be surprised that this
was mentioned.) The problem was actually worse than with the
<tt class="literal">java.sql</tt> package since many parts of the
system delivering differing types of functionality threw this same
exception, and there was no equivalent of the well established (and
fixed) set of <tt class="classname">SQLState</tt> values to deal
with.</p>
<p>To address this we concluded that there needed to be guidance
covering the choice of the specific exception to use. By requiring
any exceptions specified in throws specifications to belong to the
package that propagates them we hoped to discourage this habit.
And, by checking conformance to the guidelines as part of the class
design review, we encouraged a careful consideration of the
contract between client code and implementation.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e166" id="d0e166"></a>Exceptions
policy</h2>
</div>
<p>Following the review meeting the team adopted the suggested
policy document. (It was updated to clarify it then and a couple of
times later, but has remained close to the original discussion.) It
reads as follows:</p>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e171" id="d0e171"></a>Background</h3>
</div>
<p>There is at present no clear-cut policy for Java Exception
Handling within any of the current OPUS Java systems. This has
caused inconsistencies in the use of Exception Handling and these
have resulted in problems.</p>
<p>This document addresses the use of two categories of exceptions:
checked exceptions and unchecked exceptions.</p>
<p>Checked exceptions provide a mechanism for ensuring that the
caller of a method deals with the issue they report. (Either by
explicitly handling the exception, or by propagating it.)</p>
<p>Unchecked exceptions should only be considered for
&quot;longdistance&quot; exception propagation. (To enable reporting of
fairly catastrophic events within the system.)</p>
<p>To support these options all exceptions raised within the system
will be subclasses of either <tt class=
"classname">OpusException</tt> (which extends <tt class=
"classname">Exception</tt>) or of <tt class=
"classname">OpusRuntimeException</tt> (which extends <tt class=
"classname">RuntimeException</tt>). These provided the facility to
wrap exceptions.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e196" id="d0e196"></a>Guidelines</h3>
</div>
<p>It is the responsibility of the Class Designer to identify
issues that would result in a checked exception being thrown from a
class method. Those reviewing the class design check that this has
been done correctly. Exception specifications are not changed
during implementation without first seeking agreement that the
class design is in error.</p>
<p>Exceptions that propagate from public methods are expected to be
of types that belong to the package containing the method.</p>
<p>Within a package there are distinct types of exception for
distinct issues.</p>
<p>If a checked exception is thrown (to indicate an operation
failure) by a method in one package it is not to be propagated by a
calling method in a second package. Instead the exception is caught
and &quot;translated&quot;. Translation converts the exception into: an
appropriate return status for the method, a checked exception
appropriate to the calling package or an unchecked exception
recognised by the system. (Translation to another exception type
frequently involves &quot;wrapping&quot;.)</p>
<p>Empty catch-blocks are not used to &quot;eat&quot; or ignore exceptions.
In the rare cases where ignoring an exception is correct the empty
statement block contains a comment that makes the reasoning behind
ignoring the exception clear.</p>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e209" id="d0e209"></a>In
practice</h2>
</div>
<p>This policy seems to have worked well both in terms of being
followed without much difficulty by the original team members and
for induction of new team members. Subsequently, other teams have
also adopted it. To that extent it has been a success. However, it
isn't the end of problems with the use of exceptions.</p>
<p>The remaining problems are much more manageable and fall into
two categories:]</p>
<p>Catching exceptions at too low a level - rather than allowing
them to propagate, they are caught by a piece of code that doesn't
have sufficient context to deal with them effectively; and,</p>
<p>Catching too general a range of exceptions - for example, rather
than catching <tt class="classname">OpusException</tt> and
<tt class="classname">SQLException</tt> separately, and handling
each, there is a single catch clause for <tt class=
"classname">Exception</tt> that then uses <tt class=
"literal">instanceOf</tt> to identify the exception type. Sometimes
such code fails to take account of the possibility of <tt class=
"classname">RuntimeException</tt>s - and &quot;eats&quot; them.</p>
<p>These problems are not as widespread as those reported
originally - indeed the majority of developers on the project are
unaware of them. Both of these issues reflect poor coding technique
and can be addressed by educating developers. This education could
have occurred seamlessly as part of the project had we instituted
code reviews; but I chose to postpone introducing them in favour of
other process changes.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e237" id=
"d0e237"></a>Conclusion</h2>
</div>
<p>Java developers are rightly encouraged to use unchecked
exceptions with caution. However, the current wisdom is too
extreme. Unchecked exceptions in Java correspond to exceptions in
C++, Smalltalk and C# - and shold be used in the same way:
sparingly.</p>
<p>It seems that I'm not the only one to have doubts about this.
Shortly after I wrote the first draft to this article Kevlin Henney
pointed out that Bruce Eckel had opened discussion on the same
point [<a href="#Eckel">Eckel</a>].</p>
</div>
<div class="bibliography">
<div class="titlepage">
<h2><a name="d0e247" id="d0e247"></a>References</h2>
</div>
<div class="bibliomixed"><a name="Eckel2000" id="Eckel2000"></a>
<p class="bibliomixed">[Eckel2000] Bruce Eckel, <span class=
"citetitle"><i class="citetitle">Thinking in Java</i></span>,
Prentice-Hall Inc, 2000, ISBN 0130273635</p>
</div>
<div class="bibliomixed"><a name="Eckel" id="Eckel"></a>
<p class="bibliomixed">[Eckel] Bruce Eckel, &quot;Does Java need Checked
Exceptions?&quot;, <span class="bibliomisc"><a href=
"http://www.mindview.net/Etc/Discussions/CheckedExceptions" target=
"_top">http://www.mindview.net/Etc/Discussions/CheckedExceptions</a></span></p>
</div>
<div class="bibliomixed"><a name="Griffiths1999" id=
"Griffiths1999"></a>
<p class="bibliomixed">[Griffiths1999] Alan Griffiths, &quot;Here be
Dragons&quot;, <span class="citetitle"><i class="citetitle">Overload
40</i></span> or <span class="bibliomisc"><a href=
"http://www.octopull.demon.co.uk/c++/dragons/" target=
"_top">http://www.octopull.demon.co.uk/c++/dragons/</a></span></p>
</div>
<div class="bibliomixed"><a name="Griffiths2001" id=
"Griffiths2001"></a>
<p class="bibliomixed">[Griffiths2001] Alan Griffiths, &quot;Exception
Safe Java&quot;, ACCU Spring Conference, 2001</p>
</div>
<div class="bibliomixed"><a name="Stroustrup1990" id=
"Stroustrup1990"></a>
<p class="bibliomixed">[Stroustrup1990] Bjarne Stroustrup,
<span class="citetitle"><i class="citetitle">The Annotated C++
Reference Manual</i></span>, Addison-Wesley, 1990</p>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
