Professional Documents
Culture Documents
7,244,410 members and growing! (22,895 online) Member 2860991 Sign out
Home Articles Questions & Answers Learning Zones Jobs Features Help! Lounge
By Sohail Maroof, Farrukh Hashmi | 3 Jun 2010 | Unedited WinXP, Win2003, SQL2005,
contribution Part of The SQL Zone. VS2008, C#, ASP.NET,
Windows, SQL-Server ...
Is your email address OK? You are signed up for our newsletters but your email address is either
unconfirmed, or has not been reconfirmed in a long time. Please click here to have a confirmation email sent so
we can confirm your email address and start sending you newsletters again. Alternatively, you can update your
subscriptions.
Introduction
We have been thinking to work on Exception handling in 3-Tier Architecture using Enterprise Library
since ages but never got time or get into the situation where it should have become a requirement.
Finally, we got chance to work on it.
Our main objective was to handle exceptions seamlessly in three tiered architecture. We checked
internet but couldn’t find appropriate code sample doing stuff that we wanted. Therefore, we decided to
write a re-useable code to help ourselves and others. Below is what we think, exception handling should
do:
Based upon some certain validation Thrown Custom Error message should be
http://www.codeproject.com/KB/exception/expceptionhandling-3-tier.aspx 8/13/2010
Exception handling in 3-Tier Architecture - CodeProject Page 2 of 8
check / business rule a Custom Error is propagated to UI without Logging assuming these
3 raised from Stored Procedure. error messages will be some kind of alternative
flows instead of real exception.
While processing the request an error
System should log the thrown exception in a sink
occurred in Business Logic such as
4 (Text File). Replace the Exception with a User
Divide-by-zero, Object not set, Null
friendly Message and should propagate that to UI.
reference exception etc.
Thrown Custom Error message should be
Based upon some certain business
propagated to UI without Logging assuming these
5 validation check, a custom exception
error messages will be some kind of alternative
was thrown from Business Logic Layer.
flows instead of real exception.
While processing the request an error System should log the thrown exception in a sink
6 occurred in UI. For example, Null (Text File). Replace the Exception with a User
reference exception etc. friendly Message and should propagate that to UI.
To handle scenarios described above, following Exception Type classes are being created:
BaseException: It is a base class inherited from System.Exception class. All other classes in this
project will be inherited from this class.
namespace Common.Helpers.ExceptionHandling
{
public class BaseException : System.Exception, ISerializable
{
public BaseException()
: base()
{
// Add implementation (if required)
}
http://www.codeproject.com/KB/exception/expceptionhandling-3-tier.aspx 8/13/2010
Exception handling in 3-Tier Architecture - CodeProject Page 3 of 8
DataAccessException: This is used by the Exception handling Block to replace the original exception
with a User Friendly message such as “An unknown error has occurred at database level while
processing your request. Please contact Technical Help Desk with this identifier XXXXXXXXXXXXXX”.
namespace Common.Helpers.ExceptionHandling
{
public class DataAccessException : BaseException, ISerializable
{
public DataAccessException()
: base()
{
// Add implementation (if required)
}
}
}
DataAccessCustomException: This is used by the Exception Handling block to send raised Custom
Error message towards UI.
BusinessLogicException: This is used by the Exception Handling block to replace the original
exception with a user friendly message such as “An unknown error has occurred in Business Logic Layer
while processing your request. Please contact Technical Help Desk with this identifier
XXXXXXXXXXXXXX”.
BusinessLogicCustomException: This is used by the Exception Handling block to send raised Custom
Error message towards UI.
UserInterfaceException: This is used by the Exception Handling block to replace the original exception
with a user friendly message such as “An unknown error has occurred at User Interface while processing
your request. Please contact Technical Help Desk with this identifier XXXXXXXXXXXXXX”
DataAccessPolicy: This policy will log the original exception in defined sink will replace the original
exception with DataAccessException type.
DataAccessCustomPolicy: This policy will only replace the original exception with
http://www.codeproject.com/KB/exception/expceptionhandling-3-tier.aspx 8/13/2010
Exception handling in 3-Tier Architecture - CodeProject Page 4 of 8
DataAccessCustomException type.
PassThroughPolicy: This policy will replace the original exceptions of type DataAccessException or
DataAccessCustomException with PassThroughException type.
BusinessLogicPolicy: This policy will log the original exception in defined sink will replace the original
exception with BusinessLogicException type.
BusinessLogicCustomPolicy: This policy will only replace the original exception with
BusinessLogicCustomException type.
UserInterfacePolicy: This policy will log the original exception in defined sink will replace the original
exception with UserInterfaceException type.
http://www.codeproject.com/KB/exception/expceptionhandling-3-tier.aspx 8/13/2010
Exception handling in 3-Tier Architecture - CodeProject Page 5 of 8
<add name="UserInterfacePolicy">
<exceptionTypes>
<add name="All Exceptions" type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
postHandlingAction="ThrowNewException">
<exceptionHandlers>
<add name="UserInterfaceReplaceHandler" type="Microsoft.Practices.EnterpriseLibrary
logCategory="General" eventId="100" severity="Error" title="Enterprise Library Exception Handling"
formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrar
priority="0" />
<add name="Replace Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHa
exceptionMessage="An error occord at front end. please check."
replaceExceptionType="Common.Helpers.ExceptionHandling.UserInterfaceException, Helpers, Version=1.0.0.0, Culture=neutral, PublicKey
</exceptionHandlers>
</add>
</exceptionTypes>
</add>
Following exception handlers are being created to manage exception logging, replacement and
propagating:
namespace Common.Helpers.ExceptionHandling
{
public static class DataAccessExceptionHandler
{
public static bool HandleException(ref System.Exception ex)
{
bool rethrow = false;
if ((ex is SqlException))
{
SqlException dbExp = (SqlException)ex;
if (dbExp.Number >= 50000)
{
rethrow = ExceptionPolicy.HandleException(ex, "DataAccessCustomPolicy");
ex = new DataAccessCustomException(ex.Message);
}
else
{
rethrow = ExceptionPolicy.HandleException(ex, "DataAccessPolicy");
if (rethrow)
{
throw ex;
}
}
}
else if (ex is System.Exception)
{
rethrow = ExceptionPolicy.HandleException(ex, "DataAccessPolicy");
if (rethrow)
{
throw ex;
}
}
return rethrow;
}
}
}
http://www.codeproject.com/KB/exception/expceptionhandling-3-tier.aspx 8/13/2010
Exception handling in 3-Tier Architecture - CodeProject Page 6 of 8
using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Common.Helpers.ExceptionHandling;
namespace Common.Helpers.ExceptionHandling
{
public static class BusinessLogicExceptionHandler
{
public static bool HandleExcetion(ref System.Exception ex)
{
bool rethrow = false;
if ((ex is DataAccessException) || (ex is DataAccessCustomException))
{
rethrow = ExceptionPolicy.HandleException(ex, "PassThroughPolicy");
ex = new PassThroughException(ex.Message);
}
else if (ex is BusinessLogicCustomException)
{
rethrow = ExceptionPolicy.HandleException(ex, "BusinessLogicCustomPolicy");
}
else
{
rethrow = ExceptionPolicy.HandleException(ex, "BusinessLogicPolicy");
}
if (rethrow)
{
throw ex;
}
return rethrow;
}
}
}
If you examine the above code, you will see that in case of DataAccessException or
DataAccessCustomException, this handler is using PassThroughExceptionPolicy. Means, exception has
already been logged it needs to be propagated towards caller. Afterwards, system is checking whether
exception is raised intentionally by the Business Logic Layer or it is a uncontrolled exception. Based
upon type handler is calling appropriate Exception Handling Policy.
namespace Common.Helpers.ExceptionHandling
{
public static class UserInterfaceExceptionHandler
{
That's it. Download the code and enjoy and do leave your feedback.
Thank!
Sohail Maroof Naushahi, Farrukh Hashmi
http://www.codeproject.com/KB/exception/expceptionhandling-3-tier.aspx 8/13/2010
Exception handling in 3-Tier Architecture - CodeProject Page 7 of 8
UIControlSuite .NET
32 powerful .NET controls for
WinForms, WPF,...
United States www.binarymission.co.uk
Member
Farrukh Hashmi
See Also...
Creating Custom Controls
United States An introduction to creating custom
controls...
Member HTML 4.01 Programmer's Reference -
Chapter 1: Introduction
Using the <object> tag to embed
Article Top Rate this article for us! Poor Excellent Vote
functionality...
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+PgUp/PgDown to switch pages.
PermaLink | Privacy | Terms of Use Copyright 2010 by Sohail Maroof, Farrukh Hashmi
Last Updated: 3 Jun 2010 Everything else Copyright © CodeProject, 1999-2010
Web21 | Advertise on the Code Project
http://www.codeproject.com/KB/exception/expceptionhandling-3-tier.aspx 8/13/2010
Exception handling in 3-Tier Architecture - CodeProject Page 8 of 8
http://www.codeproject.com/KB/exception/expceptionhandling-3-tier.aspx 8/13/2010