<?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>Groschengedanken zu Software &#187; ExceptionHandling</title>
	<atom:link href="http://aysx.de/eingroschen/?feed=rss2&#038;tag=exceptionhandling" rel="self" type="application/rss+xml" />
	<link>http://aysx.de/eingroschen</link>
	<description>... und vielleicht auch dem ganzen Rest</description>
	<lastBuildDate>Tue, 18 Jun 2019 17:59:37 +0000</lastBuildDate>
	<language>de-DE</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.2.38</generator>
	<item>
		<title>Resume Next &#8211; in java</title>
		<link>http://aysx.de/eingroschen/?p=581</link>
		<comments>http://aysx.de/eingroschen/?p=581#comments</comments>
		<pubDate>Mon, 09 Feb 2015 18:40:43 +0000</pubDate>
		<dc:creator><![CDATA[matthias]]></dc:creator>
				<category><![CDATA[Allgemein]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[ExceptionHandling]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://aysx.de/eingroschen/?p=581</guid>
		<description><![CDATA[Summary Lotus Script has a nice feature for exception handling called &#8220;resume next&#8221;. Java is missing this feature. This article shows an OOP way to implement it when looping an array. The Task Imagine you have a class UrlsContentCollector. You &#8230; <a href="http://aysx.de/eingroschen/?p=581">Weiterlesen <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Summary</p>
<p>Lotus Script has a nice feature for exception handling called <a title="manpage resume next" href="http://www-12.lotus.com/ldd/doc/domino_notes/Rnext/help6_designer.nsf/f4b82fbb75e942a6852566ac0037f284/072fcc7d8dec465185256c54004c3e8d?OpenDocument">&#8220;resume next&#8221;</a>. Java is missing this feature. This article shows an OOP way to implement it when looping an array.</p>
<p>The Task</p>
<p>Imagine you have a class <code>UrlsContentCollector</code>. You provide an <code><a href="http://docs.oracle.com/javase/7/docs/api/java/net/URL.html">URL</a>[]</code> and the collector gives you all the content of the given urls in one big chunk. What is your idea about handling exceptions here? Consider this: One of your applications named &#8220;Nippi&#8221; using the <code>UrlsContentCollector</code> is picky about the content. This application will produce bogus results, if there is any error in fetching the content e.g. one of the given urls is unreachable. There is another application called &#8220;Bob&#8221;. &#8220;Bob&#8221; is a little indifferent. One url unreachable? What the heck! Write a log entry and go on.</p>
<p>On the one hand you need to throw exceptions because of &#8220;Nippi&#8221;. But this will disturb &#8220;Bob&#8221;. &#8220;Bob&#8221; can decide to ignore the exceptions but it cannot get back to the point where the exceptions was raised to continue processing the list of urls.</p>
<p>Do you think about implementing <code>UrlsContentCollector</code> twice? A &#8220;noisy&#8221; implementation and a &#8220;quiet&#8221; one? Using <a title="oracle's tutorial about reflections" href="http://docs.oracle.com/javase/tutorial/reflect/">reflections</a> and if-blocks in order to decide what to do? I think there is a better way. Or two.</p>
<p>The Clients Decide</p>
<p>&#8220;Nippi&#8221; and &#8220;Bob&#8221; both use <code>UrlsContentCollector</code>, let&#8217;s call them clients. My idea is this: The <code>UrlsContentCollector</code> does not throw an exception but it notifies the client of the exception. In response the client tells the <code>UrlsContentCollector</code> wether to stop processing or not.</p>
<p>This is easy! Notification is done via a pattern called &#8220;<a title="Wikipedia about Observer pattern" href="http://en.wikipedia.org/wiki/Observer_pattern">observer</a>&#8220;. Its interface needs a method with an exception as parameter and a boolean in its answer. &#8220;Nippi&#8221; and &#8220;Bob&#8221; use the <code>ExceptionObserver</code>. Being notified they react differently. <code>UrlsContentCollector</code> has a chance to resume if the client wants it to.</p>
<p>Your Exception</p>
<p>&#8220;Nippi&#8221; and &#8220;Bob&#8221; do not care about which item caused the problem. &#8220;Bob&#8221;&#8216;s attitude is &#8220;Stop bothering me with details &#8211; just go on&#8221;. If &#8220;Nippi&#8221; and &#8220;Bob&#8221; take a little bit more of a care, then there is an other possibility. Let&#8217;s imagine that <code>UrlsContentCollector</code> takes an array of URLs and a starting position i to process the array from i to the end. Further imagine your own exception class <code>ListprocessingInterruptedException</code>. <code>UrlsContentCollector</code> throws a <code>ListprocessingInterruptedException</code> containing the current position processed. The client catching the exception knows where to start over and <code>UrlsContentCollector</code> gives the opportunity to do so.</p>
<p>Conclusion</p>
<p><code>ExceptionObserver</code> is easy to implement and easy to use. There is no duplicated code necessary here. The classes are loosly coupled. Three reasons why this is a good idea. Maybe this idea is against the odds. This is not the usual way to handle exceptions in java. But as a proverb puts it: &#8220;You have to swim against the tide if you want to get to the spring.&#8221;</p>
<p>Creating my own exception has an advantage over using the observer pattern. I.e. exception handling is not mixed with other kinds of behaviour. Notably it is not easily confused. As a proverb puts it: &#8220;If you are in Rome do as the Romans&#8221;. It&#8217;s easy to implement, it is <a title="Wikipedia about Do not repeat yourself" href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself">DRY</a>, it is loosly coupled.</p>
<p><a title="Source Code Resume Next" href="http://www.aysx.de/eingroschen/ResumeNext.jar">You can get some bare bones uncommented source code.</a></p>
<p>Howto decide? Convention rulz cleverness.</p>
]]></content:encoded>
			<wfw:commentRss>http://aysx.de/eingroschen/?feed=rss2&#038;p=581</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
