<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>perished.de &#187; Exception Handling</title>
	<atom:link href="http://perished.de/tag/exception-handling/feed/" rel="self" type="application/rss+xml" />
	<link>http://perished.de</link>
	<description>perished but still open minded</description>
	<lastBuildDate>Sat, 16 Jul 2011 19:37:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Anti-Patterns: Werfen und Fangen von java.lang.Exception</title>
		<link>http://perished.de/2010/02/02/anti-patterns-werfen-und-fangen-von-java-lang-exception/</link>
		<comments>http://perished.de/2010/02/02/anti-patterns-werfen-und-fangen-von-java-lang-exception/#comments</comments>
		<pubDate>Tue, 02 Feb 2010 20:36:41 +0000</pubDate>
		<dc:creator>Phil</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Anti Patterns]]></category>
		<category><![CDATA[Exception Handling]]></category>
		<category><![CDATA[Exceptions]]></category>

		<guid isPermaLink="false">http://blog.perished.de/?p=1405</guid>
		<description><![CDATA[Immer wieder entdeckt man als Java-Entwickler Code-Abschnitte in dem ein &#8220;Exception Handling&#8221; verbaut ist. Das ist erfreulich und auch gut so. Exception Handling erlaubt es auf Fehler die zur Laufzeit der Software auftreten zu reagieren. Manchmal entdeckt man dabei jedoch Code-Stücke wie: 1 2 3 if &#40;BEDINGUNG&#41; &#123; throw new Exception&#40;&#34;An error occurred.&#34;&#41;; &#125; oder [...]]]></description>
			<content:encoded><![CDATA[<p>Immer wieder entdeckt man als Java-Entwickler Code-Abschnitte in dem ein &#8220;Exception Handling&#8221; verbaut ist. Das ist erfreulich und auch gut so. Exception Handling erlaubt es auf Fehler die zur Laufzeit der Software auftreten zu reagieren. Manchmal entdeckt man dabei jedoch Code-Stücke wie:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>BEDINGUNG<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    throw new Exception<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;An error occurred.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>oder</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">try <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// some code</span>
<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Exception e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// exception handling</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Diese Art mit der java.lang.Exception herumzuspringen birgt einige Nachteile und sogar Risiken. Warum Nachteile enstehen und welchen Risiken man sich aussetzt wenn man sein Exception Handling an der Basisklasse aller Exceptions ausrichtet, sei hier deshalb kurz aufgezeigt:</p>
<h1>Lesbarkeit</h1>
<p>Die Lesbarkeit des Codes leidet unter dem Werfen von java.lang.Exception.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">throw new CorruptFileException<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>sagt deutlich mehr aus als ein einfaches</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">throw new Exception<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Ich selbst vergleiche das Werfen von java.lang.Exception gerne damit jemandem versehentlich auf den Fuss zu treten. Das ist ungefähr so, als träte man jemanden versehentlich auf den Fuss und bekommt als Antwort einen Schlag ins Gesicht. Es geht einfach ein großer Teil der Information der Exception verloren.</p>
<h1>Angst und Unsicherheit</h1>
<p>Bei catch-Blöcken, die java.lang.Exception fangen, wird mir immer mulmig im Bauch. Es entsteht der Eindruck als wüsste der Entwickler nicht, welche Fehler in diesem Teil seiner Software auftreten können.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">try <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// some code</span>
<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Exception e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// exception handling</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Um diese Unsicherheit zu beseitigen, wird fälschlicherweise oft auf java.lang.Exception gecatcht. Das ist schlecht. Besser ist es die Exception einfach &#8220;nach oben&#8221; durchkrachen zu lassen oder nur die Exceptions zu fangen, die der zugehörige Source-Code auch werfen kann. Ein catch auf java.lang.Exception ist im Zweifelsfall immer die schlechteste Wahl, da hier auch alle Exceptions gefangen werden, die an dieser Stelle des Source-Code besser nicht gefangen werden sollten. Noch schlimmer wird es wenn auf Throwable gecatcht wird.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">try <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// some code</span>
<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Throwable t<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// exception handling</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>An dieser Stelle werden nicht nur alle Exceptions sondern auch alle Errors gefangen. Das hat dann mit Genauigkeit gar nichts mehr zu tun.</p>
<h1>Schlechtere Wartbarkeit</h1>
<p>Ein Entwickler, der für diese Software später vielleicht mal die Pflege und/oder Änderungen übernimmt und mit der Software nicht vertraut ist, der wird sich unter Umständen tot suchen, wenn das Logging nicht Klasse und Zeile des Sources mitloggt und einen catch wie in dem folgenden Beispiel macht.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">try <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Exception e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    LOG.<span style="color: #202020;">error</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Dieser Fehler kann insbesondere dann kritisch werden, wenn eine Software das für das Logging das Log4J PatternLayout verwendet, das gerade beim Loggen von Klasse und Zeile sehr viel Performance frisst und deshalb für das Logfile manchmal maximal noch Datum, Uhrzeit und Log-Level ins Pattern konfiguriert wird.</p>
<h1>Verlust des Informationsgehalts</h1>
<p>Nicht nur die Message einer Exception sondern auch der Name einer Exception kann auch einen Informationsgehalt haben und den Code leserlicher machen. So ist</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">try <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// some code</span>
<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>PrinterUnreachableException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// exception handling</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>mit deutlich mehr Information bestückt als</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">try <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// some code</span>
<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Exception e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// exception handling</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h1>RuntimeExceptions werden verschluckt</h1>
<p>Entwickler sind auch nur Menschen und Menschen passieren Fehler. Jeder Entwickler hat schon mal einen Fehler gemacht, der zum Beispiel eine NullPointerException oder eine ähnliche RuntimeException ausgelöst hat.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">try <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// some code</span>
    Person person <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    String <span style="color: #339933;">=</span> person.<span style="color: #202020;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Exception e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    LOG.<span style="color: #202020;">error</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;An error occurred.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Diese NullPointerException wird einfach verschlammt. Sie weißt auf einen Programmierfehler hin aber verschwindet weil sie unsachgemäß verschlammt wird. Der Entwickler, der diesen Source nun debuggen soll, hat keinen sinnvollen Anhaltspunkt was schief gelaufen sein könnte.</p>
<h1>Erst fangen und dann doch lieber weiter werfen</h1>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">public <span style="color: #993333;">void</span> foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> throws Exception <span style="color: #009900;">&#123;</span>
    try <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// some code</span>
    <span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Exception e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        LOG.<span style="color: #202020;">error</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        throw e<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Dieser Fall ist auch mit einem negativen Beigeschmack versehen. Auch wenn er auf den ersten Blick vielleicht völlig korrekt erscheinen mag. Die gefangene Exception wird ins Logfile geschrieben und dann weitergeworfen. Was wird wohl der Source außerhalb Methode foo machen? Richtig! Er wird die Exception irgendwo erneut fangen und mit großer Wahrscheinlichkeit noch mal im Logfile versenken. Die Exception wird 2 Mal im Logfile versenkt obwohl sie nur ein mal aufgetreten ist. Ugly hoch drei!</p>
<h1>Vertuschen</h1>
<p>Leere catch-Blöcke sind genauso gefährlich wie das Fangen von java.lang.Exception. Doch noch viel schlimmer wird das Ganze, wenn eine java.lang.Exception gefangen wird und dann zusätzlich auch noch der catch-Block leer bleibt.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">try <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// some code</span>
<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Exception e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Dieser Fall ist der Supergau. Der Fehler wird weder behandelt noch wird er jemals auffindbar. Er wird vertuscht und &#8220;das große Suchen&#8221; beginnt. Eine weitere Form des Vertuschens ist das Loggen der Exception-Message innerhalb catch-Blocks auf java.lang.Exception.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">try <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// some code</span>
<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Exception e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    LOG.<span style="color: #202020;">error</span><span style="color: #009900;">&#40;</span>e.<span style="color: #202020;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Diese Art der Vertuschung wird manchmal damit begründet, dass das Logfile nicht zu lang werden soll und/oder der Logfile Inhalt nicht mehr schön aussieht. Wobei wir dann natürlich bei der Frage wären, ob wir das Logfile überhaupt noch brauchen. Ein Logfile das &#8220;hübsch&#8221; aussieht, das ist genauso hilfreich bei einer eventuellen Fehlersuche als wenn man die Software ohne Logfile im Blindflug betreibt. Noch eine weitere Form des Vertuschens ist das einfache returnen von null im Fehlerfall.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">try <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// some code</span>
<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Exception e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Diese Möglichkeit kann zwar in einigen Fällen sinnvoll erscheinen aber weißt auf alles andere als einen guten Entwicklungstil hin. Und gerade bei umfangreicheren Methoden ist die Lösung pauschal schon falsch.</p>
<p>Wie man jetzt sicher einfach versteht, ist das direkte arbeiten mit java.lang.Exception eher so der &#8220;Holzfäller-Style&#8221; der Programmierung. Deshalb sollte man sich neben &#8220;Fail fast! Fail early! Fail loud!&#8221; auch mit den für den jeweiligen Source-Bereich passenden Exceptions arbeiten.</p>
<p>In diesem Sinne&#8230;<br />
Lasst es krachen! Aber richtig!</p>
<p>Für weiter Interessierte empfehle ich:<br />
<a href="http://today.java.net/pub/a/today/2006/04/06/exception-handling-antipatterns.html">http://today.java.net/pub/a/today/2006/04/06/exception-handling-antipatterns.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://perished.de/2010/02/02/anti-patterns-werfen-und-fangen-von-java-lang-exception/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

