<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Layers and Exceptions</title>
	<atom:link href="http://fragmental.tw/2008/12/04/layers-and-exceptions/feed/" rel="self" type="application/rss+xml" />
	<link>http://fragmental.tw/2008/12/04/layers-and-exceptions/</link>
	<description>Repeat after me: Data is code, code is data.</description>
	<pubDate>Tue, 16 Mar 2010 16:56:08 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5</generator>
		<item>
		<title>By: Rubem Azenha</title>
		<link>http://fragmental.tw/2008/12/04/layers-and-exceptions/#comment-1503</link>
		<dc:creator>Rubem Azenha</dc:creator>
		<pubDate>Fri, 02 Jan 2009 19:37:27 +0000</pubDate>
		<guid isPermaLink="false">http://fragmental.tw/?p=122#comment-1503</guid>
		<description>Shoes, when you told that the first layer who noticied the exception should log it, you mean in your example:

public class GroupRepository{
    public Group findByName(String nameToLookFor) throws RepositoryException{
        Group group;

        try{
        group groupDAO.findById(nameToLookFor);
        } catch(SQLException e){
            throw new RepositoryException("Problems in the persistence...", e);
	}
        return group;
    }
}

The catch clause should log the exception?

        } catch(SQLException e){
            magicLogger.log(e)
            throw new RepositoryException("Problems in the persistence...", e);
	}</description>
		<content:encoded><![CDATA[<p>Shoes, when you told that the first layer who noticied the exception should log it, you mean in your example:</p>
<p>public class GroupRepository{<br />
    public Group findByName(String nameToLookFor) throws RepositoryException{<br />
        Group group;</p>
<p>        try{<br />
        group groupDAO.findById(nameToLookFor);<br />
        } catch(SQLException e){<br />
            throw new RepositoryException(&#8221;Problems in the persistence&#8230;&#8221;, e);<br />
	}<br />
        return group;<br />
    }<br />
}</p>
<p>The catch clause should log the exception?</p>
<p>        } catch(SQLException e){<br />
            magicLogger.log(e)<br />
            throw new RepositoryException(&#8221;Problems in the persistence&#8230;&#8221;, e);<br />
	}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Phillip Calçado "Shoes"</title>
		<link>http://fragmental.tw/2008/12/04/layers-and-exceptions/#comment-1497</link>
		<dc:creator>Phillip Calçado "Shoes"</dc:creator>
		<pubDate>Thu, 01 Jan 2009 04:16:51 +0000</pubDate>
		<guid isPermaLink="false">http://fragmental.tw/?p=122#comment-1497</guid>
		<description>The whole point about contracts is to establish a protocol between client and server. This protocol provides the desired encapsulation as it leaks very few information about how the server is implemented. If you think that a given exception should be known to the client just make it part of the contract. 

In the other hand there are times where the contract has to be broken and maybe that's the kind of exception you are talking about. In that case bubbling up exceptions is fine but this is a broken contract, an uncommon situation often created by some failure in libraries or infrastructure your system uses.

If your client Layer has to handle and know all possible states of the server Layer then what's the point in using Layers in the first place? Just group your objects into packages for some cohesion and let them talk. The reason client Layers should not know about how the underlying Layer is implemented is encapsulation. If you require the client to know all possible states of the server Layer you have high coupling between those, what's generally something undesirable.

What Joel's article says is that all abstractions leak and I agree with that. It does not say that we should encourage leaky abstractions; it says that we have to keep in our minds that it is not feasible to design an abstraction that won't leak.

Now I'm not saying that you &lt;strong&gt;have to&lt;/strong&gt; use Layers of even any high level abstractions, but once you decide to use that it is often good to maximise cohesion and minimise coupling. Grouping classes will give you the former but it is extremely hard to get the latter without a good API for your abstractions; and a good API has a good contract.

I’ll probably post examples in an upcoming post.</description>
		<content:encoded><![CDATA[<p>The whole point about contracts is to establish a protocol between client and server. This protocol provides the desired encapsulation as it leaks very few information about how the server is implemented. If you think that a given exception should be known to the client just make it part of the contract. </p>
<p>In the other hand there are times where the contract has to be broken and maybe that&#8217;s the kind of exception you are talking about. In that case bubbling up exceptions is fine but this is a broken contract, an uncommon situation often created by some failure in libraries or infrastructure your system uses.</p>
<p>If your client Layer has to handle and know all possible states of the server Layer then what&#8217;s the point in using Layers in the first place? Just group your objects into packages for some cohesion and let them talk. The reason client Layers should not know about how the underlying Layer is implemented is encapsulation. If you require the client to know all possible states of the server Layer you have high coupling between those, what&#8217;s generally something undesirable.</p>
<p>What Joel&#8217;s article says is that all abstractions leak and I agree with that. It does not say that we should encourage leaky abstractions; it says that we have to keep in our minds that it is not feasible to design an abstraction that won&#8217;t leak.</p>
<p>Now I&#8217;m not saying that you <strong>have to</strong> use Layers of even any high level abstractions, but once you decide to use that it is often good to maximise cohesion and minimise coupling. Grouping classes will give you the former but it is extremely hard to get the latter without a good API for your abstractions; and a good API has a good contract.</p>
<p>I’ll probably post examples in an upcoming post.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chuck</title>
		<link>http://fragmental.tw/2008/12/04/layers-and-exceptions/#comment-1481</link>
		<dc:creator>Chuck</dc:creator>
		<pubDate>Wed, 24 Dec 2008 01:33:40 +0000</pubDate>
		<guid isPermaLink="false">http://fragmental.tw/?p=122#comment-1481</guid>
		<description>"The contract defines what the client Layer must expect, all exceptions that can be thrown by a Layer should be defined in the contract."

But what benefit does the client have knowing this? It seems like only a small amount of exceptions that might get thrown can be recovered from, so what can the client really do with this information?

"otherwise the client Layer would have to be able to deal with pretty much any possible situation that could happen"

I'd say this is always the case and client layers should be designed with this in mind. You even mentioned this with your reference to Joel's leaky abstraction article.

I'm still not convinced there's a significant benefit (maybe an example or reference would help). I understand your point with contracts, and it sounds nice, however I'm not convinced that you can often map every exception. With this in mind, it doesn't seem practical to map some, knowing others might leak.</description>
		<content:encoded><![CDATA[<p>&#8220;The contract defines what the client Layer must expect, all exceptions that can be thrown by a Layer should be defined in the contract.&#8221;</p>
<p>But what benefit does the client have knowing this? It seems like only a small amount of exceptions that might get thrown can be recovered from, so what can the client really do with this information?</p>
<p>&#8220;otherwise the client Layer would have to be able to deal with pretty much any possible situation that could happen&#8221;</p>
<p>I&#8217;d say this is always the case and client layers should be designed with this in mind. You even mentioned this with your reference to Joel&#8217;s leaky abstraction article.</p>
<p>I&#8217;m still not convinced there&#8217;s a significant benefit (maybe an example or reference would help). I understand your point with contracts, and it sounds nice, however I&#8217;m not convinced that you can often map every exception. With this in mind, it doesn&#8217;t seem practical to map some, knowing others might leak.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Phillip Calçado "Shoes"</title>
		<link>http://fragmental.tw/2008/12/04/layers-and-exceptions/#comment-1480</link>
		<dc:creator>Phillip Calçado "Shoes"</dc:creator>
		<pubDate>Tue, 23 Dec 2008 21:34:47 +0000</pubDate>
		<guid isPermaLink="false">http://fragmental.tw/?p=122#comment-1480</guid>
		<description>Chuck,

It is important because if you don't map the original exception to something that is specified in the contract you just broke it. The contract defines what the client Layer must expect, all exceptions that can be  thrown by a Layer should be defined in the contract.

Unless, of course, that you don't use contracts between Layers. This is other topic but I'd recommend that you limit the number of possible exceptions anyway, otherwise the client Layer would have to be able to deal with pretty much any possible situation that could happen.</description>
		<content:encoded><![CDATA[<p>Chuck,</p>
<p>It is important because if you don&#8217;t map the original exception to something that is specified in the contract you just broke it. The contract defines what the client Layer must expect, all exceptions that can be  thrown by a Layer should be defined in the contract.</p>
<p>Unless, of course, that you don&#8217;t use contracts between Layers. This is other topic but I&#8217;d recommend that you limit the number of possible exceptions anyway, otherwise the client Layer would have to be able to deal with pretty much any possible situation that could happen.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chuck</title>
		<link>http://fragmental.tw/2008/12/04/layers-and-exceptions/#comment-1476</link>
		<dc:creator>Chuck</dc:creator>
		<pubDate>Tue, 23 Dec 2008 17:50:16 +0000</pubDate>
		<guid isPermaLink="false">http://fragmental.tw/?p=122#comment-1476</guid>
		<description>"What you have to do is to map the internal exception to a contract exception."

With respect to how the exceptions are handled and logged, why does it matter if I convert the internal exception to a contract exception?

I've always applied this rule on a case by case basis. I've seen other people suggest the 'do it all the time' approach, but I've never seen a good explanation as to why. I suspect this has more to do with code ownership and who's consuming your code.

To me it seems more important to do this when exceptions move across subsystems or trust boundaries (e.g. exception shielding), and less important when moving across layers.</description>
		<content:encoded><![CDATA[<p>&#8220;What you have to do is to map the internal exception to a contract exception.&#8221;</p>
<p>With respect to how the exceptions are handled and logged, why does it matter if I convert the internal exception to a contract exception?</p>
<p>I&#8217;ve always applied this rule on a case by case basis. I&#8217;ve seen other people suggest the &#8216;do it all the time&#8217; approach, but I&#8217;ve never seen a good explanation as to why. I suspect this has more to do with code ownership and who&#8217;s consuming your code.</p>
<p>To me it seems more important to do this when exceptions move across subsystems or trust boundaries (e.g. exception shielding), and less important when moving across layers.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: AC de Souza</title>
		<link>http://fragmental.tw/2008/12/04/layers-and-exceptions/#comment-1445</link>
		<dc:creator>AC de Souza</dc:creator>
		<pubDate>Wed, 10 Dec 2008 12:25:22 +0000</pubDate>
		<guid isPermaLink="false">http://fragmental.tw/?p=122#comment-1445</guid>
		<description>@Tim

First of all, I would like to sorry my argumenting. But, I REALLY intrested in opinions about this topic.

1) This monitoring agent should't be in the server? Or, maybe, you could worry when you map a server exception to a client exception that your client monitoring agent understand, or using an Exception's Code.

2) This could be done with an Exception's Code, in the Client Exception, instead a Server Exception. Something the HTTP code 502: Service Temporarily Overloaded. As java.sql.SQLException do: venderCode.

3) In this case I need to know the exactaly developer's intend(what is he looking for?) to propouse a solution. But, if its a IDE debug, de developer always can put a breakpoint in the caller method :)

[],
AC</description>
		<content:encoded><![CDATA[<p>@Tim</p>
<p>First of all, I would like to sorry my argumenting. But, I REALLY intrested in opinions about this topic.</p>
<p>1) This monitoring agent should&#8217;t be in the server? Or, maybe, you could worry when you map a server exception to a client exception that your client monitoring agent understand, or using an Exception&#8217;s Code.</p>
<p>2) This could be done with an Exception&#8217;s Code, in the Client Exception, instead a Server Exception. Something the HTTP code 502: Service Temporarily Overloaded. As java.sql.SQLException do: venderCode.</p>
<p>3) In this case I need to know the exactaly developer&#8217;s intend(what is he looking for?) to propouse a solution. But, if its a IDE debug, de developer always can put a breakpoint in the caller method <img src='http://fragmental.tw/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>[],<br />
AC</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Timothy High</title>
		<link>http://fragmental.tw/2008/12/04/layers-and-exceptions/#comment-1444</link>
		<dc:creator>Timothy High</dc:creator>
		<pubDate>Tue, 09 Dec 2008 18:59:37 +0000</pubDate>
		<guid isPermaLink="false">http://fragmental.tw/?p=122#comment-1444</guid>
		<description>Responding to @AC,
There are a number of reasons why you might want to get down to the cause of an Exception, not all of them valid from a purely theoretical standpoint:

1) The client has some sort of monitoring / error reporting facility that sends any server exceptions to an admin via email, via a monitoring agent, or some other means.

2) The client may want to automatically respond to certain kinds of server-side exceptions. We have defined a sub-type of Exception called "Retriable" (say, when a server is overloaded, or has run out of DB connections). When an exception of this type is thrown, the client can check if the "cause" is of this type, and can retry the request without bothering the user.

3) A developer may be manually debugging or running a profiling tool that wants to have a look at the innards of the Exception. It would be a bummer if the act of inspecting the object causes it to blow up in your face.

@Phillip,
Right, that's basically the point I was trying to make. Just a reminder that wrapping an Exception, while usually helpful, may actually be breaking encapsulation and causing problems in unexpected ways.</description>
		<content:encoded><![CDATA[<p>Responding to @AC,<br />
There are a number of reasons why you might want to get down to the cause of an Exception, not all of them valid from a purely theoretical standpoint:</p>
<p>1) The client has some sort of monitoring / error reporting facility that sends any server exceptions to an admin via email, via a monitoring agent, or some other means.</p>
<p>2) The client may want to automatically respond to certain kinds of server-side exceptions. We have defined a sub-type of Exception called &#8220;Retriable&#8221; (say, when a server is overloaded, or has run out of DB connections). When an exception of this type is thrown, the client can check if the &#8220;cause&#8221; is of this type, and can retry the request without bothering the user.</p>
<p>3) A developer may be manually debugging or running a profiling tool that wants to have a look at the innards of the Exception. It would be a bummer if the act of inspecting the object causes it to blow up in your face.</p>
<p>@Phillip,<br />
Right, that&#8217;s basically the point I was trying to make. Just a reminder that wrapping an Exception, while usually helpful, may actually be breaking encapsulation and causing problems in unexpected ways.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: links for 2008-12-05 &#171; Object neo = neo Object</title>
		<link>http://fragmental.tw/2008/12/04/layers-and-exceptions/#comment-1441</link>
		<dc:creator>links for 2008-12-05 &#171; Object neo = neo Object</dc:creator>
		<pubDate>Sat, 06 Dec 2008 04:31:07 +0000</pubDate>
		<guid isPermaLink="false">http://fragmental.tw/?p=122#comment-1441</guid>
		<description>[...] Layers and Exceptions at Fragmental.tw return new Event(&#34;Exception blog post found&#34;); (tags: java exceptions) [...]</description>
		<content:encoded><![CDATA[<p>[&#8230;] Layers and Exceptions at Fragmental.tw return new Event(&quot;Exception blog post found&quot;); (tags: java exceptions) [&#8230;]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: AC de Souza</title>
		<link>http://fragmental.tw/2008/12/04/layers-and-exceptions/#comment-1440</link>
		<dc:creator>AC de Souza</dc:creator>
		<pubDate>Fri, 05 Dec 2008 19:38:57 +0000</pubDate>
		<guid isPermaLink="false">http://fragmental.tw/?p=122#comment-1440</guid>
		<description>@Tim and @Philip
 I was thinking about "exception encapsulation" and concludes that:
-  If you're mapping one, or a group, of exception there is no reason to encapsulate the original.

You should log the problem when it happened and throw an exception which your client can understand and handle. So the original exception has no meaning to the client, if he can't handle it.

But if your client could handle, why don't you throw the original exception instead some pseudo-abstraction?

And if your client can handle, doesn't him doing more than he supossed to?

[],
AC</description>
		<content:encoded><![CDATA[<p>@Tim and @Philip<br />
 I was thinking about &#8220;exception encapsulation&#8221; and concludes that:<br />
-  If you&#8217;re mapping one, or a group, of exception there is no reason to encapsulate the original.</p>
<p>You should log the problem when it happened and throw an exception which your client can understand and handle. So the original exception has no meaning to the client, if he can&#8217;t handle it.</p>
<p>But if your client could handle, why don&#8217;t you throw the original exception instead some pseudo-abstraction?</p>
<p>And if your client can handle, doesn&#8217;t him doing more than he supossed to?</p>
<p>[],<br />
AC</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Phillip Calçado "Shoes"</title>
		<link>http://fragmental.tw/2008/12/04/layers-and-exceptions/#comment-1438</link>
		<dc:creator>Phillip Calçado "Shoes"</dc:creator>
		<pubDate>Fri, 05 Dec 2008 00:05:23 +0000</pubDate>
		<guid isPermaLink="false">http://fragmental.tw/?p=122#comment-1438</guid>
		<description>@Antonio Carlos	Zegunis

I don't	understand how multiple	Layers would log the exception.	The first one to receive it, the one who generates the exception or receives it from a subsystem or library (*not from other Layer*) will log that.

About logging in the last Layer, this is helpful as stack traces can give you the full path. Problem is that although it is very easy to know who is the first Layer it is hard to realise who is the last Layer. If a Layer knows that it is the last one it knows too much about the system, Layers should be atomic and independent.

@Tim

Besides the point raised by AC about not getting into the exception it feels like what you are having is the classic DTO/local objects problem. In this case "mapping" may mean detaching it from the server.</description>
		<content:encoded><![CDATA[<p>@Antonio Carlos	Zegunis</p>
<p>I don&#8217;t	understand how multiple	Layers would log the exception.	The first one to receive it, the one who generates the exception or receives it from a subsystem or library (*not from other Layer*) will log that.</p>
<p>About logging in the last Layer, this is helpful as stack traces can give you the full path. Problem is that although it is very easy to know who is the first Layer it is hard to realise who is the last Layer. If a Layer knows that it is the last one it knows too much about the system, Layers should be atomic and independent.</p>
<p>@Tim</p>
<p>Besides the point raised by AC about not getting into the exception it feels like what you are having is the classic DTO/local objects problem. In this case &#8220;mapping&#8221; may mean detaching it from the server.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
