    <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  :: Java Style &amp; Idioms : exceptions are fun</title>
        <link>https://members.accu.org/index.php/journals/869</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 11, #3 - Apr 1999 + 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/c132/">113</a>
                    (22)
<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/c132-65/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/journals/c132+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;Java Style &amp; Idioms : exceptions are fun</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 03 April 1999 13:15:30 +01:00 or Sat, 03 April 1999 13:15:30 +01:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="section" lang="en">
<div class="titlepage">
<h2><a name="d0e20" id="d0e20"></a></h2>
</div>
<div class="variablelist">
<dl>
<dt><span class="term">Name:</span></dt>
<dd>
<p>exceptions are fun</p>
</dd>
<dt><span class="term">Rationale:</span></dt>
<dd>
<p>Exceptions can be fun! Throwing and catching them in a sensible
manner can help both yourself and other users of your code.</p>
</dd>
<dt><span class="term">Advantages:</span></dt>
<dd>
<p>Can help to write more robust and easier to debug code.</p>
</dd>
<dt><span class="term">Disadvantages:</span></dt>
<dd>
<p>None.</p>
</dd>
<dt><span class="term">Description:</span></dt>
<dd>
<p>I have come across the following appalling code in some Java
books:</p>
<pre class="programlisting">
try
  {
    // do lots of possibly dodgy stuff ...
  }
  catch (Exception e)
  {
    // junk the exceptions!
  }
an even worse example would be:
  try
  {
    // do lots of possibly dodgy stuff ...
  }
  catch (Throwable t)
  {
    // junk the throwables!
  }
</pre>
<p>these are really bad examples because they appear in books that
claim to be teaching programmers how to write Java! The reason that
I think this is bad practice is because the ultimate user is
actually being prevented from seeing the underlying reason for
failures. If they are lucky they'll get to see an error message of
the form &quot;operation failed&quot;. If they are unlucky they'll have no
error message and a program that fails &quot;silently&quot;. I am really fed
up with &quot;useless&quot; error messages that cause me more pain than gain!
(Especially when system vendors go out of their way to fully
document their errors - NOT!)</p>
<p>The simplest way to fix the above code (assuming we have a
command line based application) is to do the following relatively
easy change:</p>
<pre class="programlisting">
  try
  {
    // do lots of possibly dodgy stuff ...
  }
  catch (Exception e)
  {
    e.printStackTrace();   // report the exceptions!
  }
</pre>
<p>Voila! The user now knows what went wrong. Such a simple little
change. Sometimes the stack trace is incomplete because of
JIT'ting. To get a complete stack trace [i.e. one with line
numbers] you have to run the code with the JIT disabled.</p>
<p>The reason that you should generally not catch <tt class=
"classname">Throwable</tt> is because the JVM uses the <tt class=
"classname">Error</tt> subclass to specify abnormal conditions that
should &quot;never&quot; arise. JavaSoft actually recommends that programmers
should not trap these abnormal conditions. If you do catch them
then you'll find that you don't really have many options left!
Better leave them to the JVM&#9786;</p>
<p>An interesting question is how to handle exceptions. The
handling mechanism that one chooses would depend on the context of
the code. If the code is part of a library then I think the only
sensible approach is to pass all exceptions back to the user of the
library. This is especially true of exceptions originating from the
<tt class="literal">java.*</tt> packages [e.g. <tt class=
"classname">java.io.IOException</tt>]. The library author could
also invent library specific exceptions applicable to the library
that should be the user's responsibility to deal with.</p>
<p>If the code is part of a standalone application [GUI or command
line] then exceptions should ultimately be visible to the user.
With a command line based utility I would generally expect the full
stack trace to be sufficient. One has greater freedom in the case
of a GUI based application. An error-based popup can be displayed
giving the useless &quot;operation failed&quot; message as long as the user
can click on a button in the dialog box [Help/More info?] that
would give the full stack trace. In either case it is usual to have
supplied a context-based string to the exception constructor to
make the underlying reason for the exception obvious!</p>
<p>Another interesting question is where to handle exceptions? The
answer, of course, is that it really depends on what you are doing!
In standalone applications I tend to have a catch all in my main
method with a simple <tt class="methodname">printStackTrace</tt>
invocation. In a GUI based application I would expect the major
work methods to have catch-alls with a dialog box display for the
user. Individual methods may catch specific exceptions providing
they handle them! One such exception is <tt class=
"classname">InterruptedException</tt> from <tt class=
"methodname">Thread.sleep()</tt> I would normally expect this to be
caught and either ignored as being of no real consequence or else
acted upon in some manner. Note that it is acceptable to ignore it
in this case because it is a specific exception under a specific
circumstance.</p>
<p>I actually love exceptions! I think they make code easier to
read. They also make it simpler to debug and maintain. When people
report &quot;problems&quot; with my code I can usually tell what the problem
is JUST by inspecting the exception! [Normally the runtime
environment has not been correctly set up].</p>
<p>The idiom is to either catch and handle an exception sensibly or
else to report it sensibly. Don't just junk them and fail
silently!</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>
