<?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>Learning by Experience &#187; architecture</title> <atom:link href="http://www.inze.be/andries/tag/architecture/feed/" rel="self" type="application/rss+xml" /><link>http://www.inze.be/andries</link> <description>Java, Project Management, Life and anything else.</description> <lastBuildDate>Mon, 09 Jan 2012 21:38:00 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <item><title>The BusinessException pattern</title><link>http://www.inze.be/andries/2008/08/18/the-businessexception-pattern/</link> <comments>http://www.inze.be/andries/2008/08/18/the-businessexception-pattern/#comments</comments> <pubDate>Mon, 18 Aug 2008 21:08:58 +0000</pubDate> <dc:creator>Andries Inzé</dc:creator> <category><![CDATA[architecture]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[patterns]]></category> <guid
isPermaLink="false">http://www.inze.be/andries/?p=41</guid> <description><![CDATA[This exception is probably one of the most common home-made exceptions, one that pops up in almost every project. I'll try and show you how it works and what the advantages are.]]></description> <content:encoded><![CDATA[<p>This exception is probably one of the most common<em> home-made</em> exceptions, one that pops up in almost every project. I&#8217;ll try and show you how it works and what the advantages are.</p><p>One of the &#8220;problems&#8221; of doing all the business logic in the Service (or UseCase, Business) Layer is getting the right exception message back onto the screen. Lot&#8217;s of times people invent all kinds of home-brewed exceptions: e.g. <em>ResultNotFoundException</em>, <em>InsufficientFundsException</em>, <em>DateTooFarIntoFutureException</em>, &#8230;<br
/> Another approach, which is far worse, is to let specialized exceptions like a <em>ConstraintViolation </em>(hibernate), <em>UserException </em>(axis) and many others. It couples the front-end to back end.</p><p>When defining custom exceptions for every problem, you force the controller to catch all the exceptions that could be thrown separately. Adding a new exception can be cumbersome, and it does not really matter if you define them as checked or unchecked, unless you plan to catch the remaining exceptions with a generic info message like: something went wrong&#8230;</p><h2>What&#8217;s the correct solution?</h2><p>Here comes the BusinessException</p><pre>public class BusinessException
    extends RuntimeException {
	/** A code indicating the cause of
            the exception. */
	private String code;
	/** The exception that is wrapped by
            the business component. */
	private Exception wrappedException;
	/**
	 * Constructor.
	 *
	 * @param code The code indicating the
         * cause of the exception.
	 * @param wrappedException The exception
         * that is wrapped by the business
         * component.
	 */
	public BusinessException(String code,
          Exception wrappedException) {
		this.code = code;
		this.wrappedException =
                  wrappedException;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public Exception getWrappedException() {
		return wrappedException;
	}
	public void setWrappedException(
           Exception wrappedException) {
		this.wrappedException = wrappedException;
	}
}</pre><p>When a business rule validation has been detected, a new BusinessException should be throw.</p><p>When an exception is thrown from any layer beneath the Controller layer, it should be wrapped by the BusinessException.</p><p>The <em>code</em> is the key for translation by the view renderer.</p><h2>Placing this in the architecture</h2><p>When we place the BusinessException into a widely use architectural stack we come up with something like this:</p><p
style="text-align: center;"><a
title="BusinessException architecture" href="http://www.inze.be/andries/wp-content/architecture.jpg" target="_blank"><img
class="aligncenter size-medium wp-image-56" title="architecture" src="http://www.inze.be/andries/wp-content/architecture-300x160.jpg" alt="" width="300" height="160" /></a></p><h2>Advantages</h2><p>The advantages are clear:</p><ul><li>The controller and other layers are loosely coupled (e.g. no DAO dependency in Controller)</li><li>The transaction can be rolled back, since the exception is thrown.</li><li>The controllers aren&#8217;t forced to catch many exceptions.</li><li>No need for those custom made exceptions. One exception to rule them all!</li></ul><h2>BusinessException versus other exceptions</h2><p>The businessException should wrap a recoverable violation or exception, that can be displayed in the screen. Transaction is rolled back, and the user can change the input and/or try again.</p><p>Normal exceptions are unrecoverable. The transaction is rolled back, and the user is directed to an error page.</p><p>It&#8217;s not the point to wrap every exception, but only the expected ones. Don&#8217;t wrap a It&#8217;s a really simple pattern, but a very powerful one.</p><p>In essence 4 scenario&#8217;s can happen:</p><ol><li>The first level fails. Typically this is something like a missing required field. The user is redirected to the same page to fix the problem.</li><li>The second validation fails: a business exception is thrown.  Typically this is a business rule that failed, for instance a balance is too low to complete a purchase. It also could be an exception wrapped by the BusinessException. For instance when the database row has changed after you loaded it.</li><li>Everything succeeded, the business layer returned normally.</li><li>The business layer threw an exception (not a BusinessException), the page is redirected to the error page. Unrecoverable, should only be on programming bug.</li></ol><h2>Conclusion</h2><p>A lot has to be said for straight and simple win-win patterns. This one is definately one of those.</p> ]]></content:encoded> <wfw:commentRss>http://www.inze.be/andries/2008/08/18/the-businessexception-pattern/feed/</wfw:commentRss> <slash:comments>7</slash:comments> </item> </channel> </rss>
