<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Mapping SQL Server Errors to .NET Exceptions (the fun way!)</title><link>http://blogs.msdn.com/tomholl/archive/2007/08/01/mapping-sql-server-errors-to-net-exceptions-the-fun-way.aspx</link><description>As I mentioned in my last post , I've been having some fun discovering what it's like to use patterns &amp;amp; practices deliverables on a real enterprise application. One of the challenges I've faced was figuring out an exception management strategy that</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Mapping SQL Server Errors to .NET Exceptions (the fun way!)</title><link>http://blogs.msdn.com/tomholl/archive/2007/08/01/mapping-sql-server-errors-to-net-exceptions-the-fun-way.aspx#4166143</link><pubDate>Wed, 01 Aug 2007 14:39:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4166143</guid><dc:creator>Peter Ritchie</dc:creator><description>&lt;p&gt;Not to mention having to write all the individual exception classes to handle Number being 1 to 21999+. &amp;nbsp;This is a corner exception handing case where the standard exception handling practices become a hindrance, obviously it would not be good to have 21000+ individual exception classes.&lt;/p&gt;
&lt;p&gt;One of the wonderful things about exception handling is the ability to rethrow new exceptions. &amp;nbsp;If an exception can't be handled but the code that caught the exception can add value to it because of it's context, one useful tool is to rethrow a new exception. &amp;nbsp;I don't really see that possibility here (except to throw a different exception from HandleException, which doesn't smell right). &amp;nbsp;I think the ability to get a new exception from HandleException (or TryHandleException) that the catch block could re-throw or do something with (since HandleException is an abstraction) could prove useful.&lt;/p&gt;
</description></item><item><title>re: Mapping SQL Server Errors to .NET Exceptions (the fun way!)</title><link>http://blogs.msdn.com/tomholl/archive/2007/08/01/mapping-sql-server-errors-to-net-exceptions-the-fun-way.aspx#4166214</link><pubDate>Wed, 01 Aug 2007 14:47:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4166214</guid><dc:creator>tomholl</dc:creator><description>&lt;p&gt;Peter - this example is all about throwing new exceptions. The Exception Handling Application Block can be configured to do any of the following actions after executing the exception handling pipeline:&lt;/p&gt;
&lt;p&gt; - Swallow exceptions (HandleException returns false)&lt;/p&gt;
&lt;p&gt; - Notify that the original exception should be rethrown (HandleException returns true)&lt;/p&gt;
&lt;p&gt; - Throw the exception coming out of the pipeline (This happens inside HandleException so nothing is returned).&lt;/p&gt;
&lt;p&gt;When using the SqlExceptionWrapHandler (or the built-in WrapHandler and ReplaceHandler), the only option that makes sense is to throw the new exception. &lt;/p&gt;
&lt;p&gt;Also I agree you wouldn't want to create 21,000 exception classes, but this solution doesn't require you to do so. In most situations there are only a handful of error codes that you will likely justify a new exception type, since, as you mentioned the whole point here is to add context that a higher layer might be able to deal with - and there is a good chance that 99% of possible SQL errors won't be dealt with individually.&lt;/p&gt;
</description></item><item><title>re: Mapping SQL Server Errors to .NET Exceptions (the fun way!)</title><link>http://blogs.msdn.com/tomholl/archive/2007/08/01/mapping-sql-server-errors-to-net-exceptions-the-fun-way.aspx#4166251</link><pubDate>Wed, 01 Aug 2007 14:51:34 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4166251</guid><dc:creator>tomholl</dc:creator><description>&lt;p&gt;I forgot to mention that from EntLib 3.0, there is a new overload to ExceptionPolicy.HandleException that returns the exception coming out of the pipeline as an out parameter, rather than automatically throwing it. I think this is exactly what you suggested Peter.&lt;/p&gt;
</description></item><item><title>re: Mapping SQL Server Errors to .NET Exceptions (the fun way!)</title><link>http://blogs.msdn.com/tomholl/archive/2007/08/01/mapping-sql-server-errors-to-net-exceptions-the-fun-way.aspx#4174026</link><pubDate>Wed, 01 Aug 2007 20:45:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4174026</guid><dc:creator>Peter Ritchie</dc:creator><description>&lt;p&gt;Tom, yes the ExceptionPolicy.HandleException overload seems to be exactly what I was talking about...&lt;/p&gt;
&lt;p&gt;With regard to 21000 exception classes; I was basically agreeing that the finally exception handling code in your blog is necessary. &amp;nbsp;It's effectively impossible to write 21000 exception classes to make to each of the SQL Server error codes. &amp;nbsp;But, in the general case it's preferred to have a error-condition-to-exception mapping.&lt;/p&gt;
</description></item><item><title>re: Mapping SQL Server Errors to .NET Exceptions (the fun way!)</title><link>http://blogs.msdn.com/tomholl/archive/2007/08/01/mapping-sql-server-errors-to-net-exceptions-the-fun-way.aspx#4203389</link><pubDate>Fri, 03 Aug 2007 11:53:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4203389</guid><dc:creator>Alex</dc:creator><description>&lt;p&gt;But if we use [ExceptionCallHandler]? How could we capture the error messages and present under the Web Pages?&lt;/p&gt;
&lt;p&gt;We still need to try.. catch to make it work if we really want to show that error message in the web page.&lt;/p&gt;
&lt;p&gt;Is this true?&lt;/p&gt;
</description></item><item><title>re: Mapping SQL Server Errors to .NET Exceptions (the fun way!)</title><link>http://blogs.msdn.com/tomholl/archive/2007/08/01/mapping-sql-server-errors-to-net-exceptions-the-fun-way.aspx#4217797</link><pubDate>Sat, 04 Aug 2007 09:33:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4217797</guid><dc:creator>tomholl</dc:creator><description>&lt;p&gt;Alex - the solution described in this post is about wrapping SqlExceptions into more meaningful .NET exceptions as they leave the data access layer. In order to display messages to users in an ASP.NET application, you'd most likely want to use a standard try... catch approach.&lt;/p&gt;
</description></item><item><title>re: Mapping SQL Server Errors to .NET Exceptions (the fun way!)</title><link>http://blogs.msdn.com/tomholl/archive/2007/08/01/mapping-sql-server-errors-to-net-exceptions-the-fun-way.aspx#4368507</link><pubDate>Mon, 13 Aug 2007 17:07:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4368507</guid><dc:creator>Herbjörn Wilhelmsen</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I would appreciate it if the ErrorCode in the handlers configuration could contain a comma separated list of values like:&lt;/p&gt;
&lt;p&gt;547,2601,2602,2603,2604,2605 &lt;/p&gt;
&lt;p&gt;or even better: &lt;/p&gt;
&lt;p&gt;547,2601-2605&lt;/p&gt;
</description></item><item><title>re: Mapping SQL Server Errors to .NET Exceptions (the fun way!)</title><link>http://blogs.msdn.com/tomholl/archive/2007/08/01/mapping-sql-server-errors-to-net-exceptions-the-fun-way.aspx#4879907</link><pubDate>Wed, 12 Sep 2007 16:53:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4879907</guid><dc:creator>Ali Kheyrollahi</dc:creator><description>&lt;p&gt;This lack of flexibility was one of the reasons I did not like EHAB in the first place. I think exception type is not granular enough for applying policies and I started writing my own exception handling block but had to abandon it because of lack of time. One of the ideas I had was a regular expression pattern to test the exception message against it but that would not scale.&lt;/p&gt;
&lt;p&gt;I am not sure if I am thinking just too wild, believe me I have not been on any marij.. but abstracting the exception into these elements can be possible:&lt;/p&gt;
&lt;p&gt;1. Offending element&lt;/p&gt;
&lt;p&gt;2. Offending operation&lt;/p&gt;
&lt;p&gt;3. Offending condition&lt;/p&gt;
&lt;p&gt;4. Check-rule element&lt;/p&gt;
&lt;p&gt;5. Code&lt;/p&gt;
&lt;p&gt;FileNotFoundException:&lt;/p&gt;
&lt;p&gt;1. Offending element: The file name&lt;/p&gt;
&lt;p&gt;2. Offending operation: access&lt;/p&gt;
&lt;p&gt;3. Offending condition: NotFound&lt;/p&gt;
&lt;p&gt;4. Check element: None&lt;/p&gt;
&lt;p&gt;5. Code: None&lt;/p&gt;
&lt;p&gt;Unique Key exception:&lt;/p&gt;
&lt;p&gt;1. Offending element: new record&lt;/p&gt;
&lt;p&gt;2. Offending operation: insert&lt;/p&gt;
&lt;p&gt;3. Offending condition: duplicate&lt;/p&gt;
&lt;p&gt;4. Check element: unique key ORDERITEM_TYPE&lt;/p&gt;
&lt;p&gt;5. Code: None&lt;/p&gt;
&lt;p&gt;COM Exception number 2147024809&lt;/p&gt;
&lt;p&gt;1. Offending element: an argument&lt;/p&gt;
&lt;p&gt;2. Offending operation: calling a method&lt;/p&gt;
&lt;p&gt;3. Offending condition: invalid&lt;/p&gt;
&lt;p&gt;4. Check element: None&lt;/p&gt;
&lt;p&gt;5. Code: 2147024809&lt;/p&gt;
&lt;p&gt;If each .NET exception could provide these elements, we could apply a lot more tules to them. It might need involvement of an NLP scientist but I think it is possible to abstract these.&lt;/p&gt;
</description></item><item><title>EntLib Contrib September 2007 Release</title><link>http://blogs.msdn.com/tomholl/archive/2007/08/01/mapping-sql-server-errors-to-net-exceptions-the-fun-way.aspx#5238004</link><pubDate>Tue, 02 Oct 2007 07:56:20 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5238004</guid><dc:creator>Tom Hollander's blog</dc:creator><description>&lt;p&gt;In keeping with the p&amp;amp;amp;p team's tradition of naming a release after the month that's just finished,&lt;/p&gt;
</description></item><item><title>5 reasons to download the EntLib Contrib September 2007 release</title><link>http://blogs.msdn.com/tomholl/archive/2007/08/01/mapping-sql-server-errors-to-net-exceptions-the-fun-way.aspx#5240368</link><pubDate>Tue, 02 Oct 2007 12:43:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5240368</guid><dc:creator>^(?:[^$]*)$ --Matches everything, captures nothing</dc:creator><description>&lt;p&gt;This morning I woke up to Tom&amp;amp;#39;s announcement on the new EntLib Contrib release. No big surprises&lt;/p&gt;
</description></item></channel></rss>