473,473 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Exception Handling Techniques for ASP.NET Application with Multiple Layers

We have an ASP.NET 2.0 (C#) application that is divided into multiple
layers. The multiple layers come from having a web project and 2 different
class library projects in the same solution.

I'm having difficulties figuring out the best way to handle (catch)
exceptions in the different layers and then propagating those errors back up
through the call stack to ultimately display something to the end-user.
Note this is an intranet application to be used in-house by company
employees; it will not be a public Internet application.

See the code snippets below for an example of how we're handling things now.

ASP.NET Application
Web.config
<system.web>
<customErrors defaultRedirect="~/ErrorPages/ExceptionHandler.aspx"
mode="On" />
</system.web>
Global.asax
void Application_Error(Object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
// Formulate a message to write to the event log
...
}
Code-behind of .aspx page
System.Data.DataSet dsFacility = null;

try
{
// Do some other processing here
...

// Get the facility info from the database
dsFacility = BusinessRules.Facility.GetFacility();

// Do some other processing here
...
}
catch (System.Exception ex)
{
// Handle the exception, log it
...

// Throw the exception to be handled higher up the call stack
throw;
}

Business Rules class (this is a separate class library project)
public static class Facility
{
public static System.Data.DataSet GetFacility()
{
// Get the facility information
System.Data.DataSet dsFacility = null;

try
{
// Do some other processing here
...

// Get the facility info from the database
dsFacility = DataAccess.Facility.GetFacility();

// Do some other processing here
...
}
catch (System.Exception ex)
{
// Handle the exception, log it, etc.
...

// Throw the exception to be handled higher up the call
stack
throw;
}

// Rows found, return the queried dataset
return dsFacility;
}
}

Data Access class (this is a separate class library project)
public static class Facility
{
public static System.Data.DataSet GetFacility()
{
// Define and initialize local variables
string sqlCommand = "";
System.Data.SqlClient.SqlConnection scFacility = null;
System.Data.SqlClient.SqlDataAdapter sdaFacility = null;
System.Data.DataSet dsFacility = null;

try
{
// Create and populate the local variables
sqlCommand = "SELECT * FROM Facility ORDER BY FacilityName";
scFacility = new
System.Data.SqlClient.SqlConnection(DatabaseConfig uration.RegPerfectDbConnec
tionString);
sdaFacility = new
System.Data.SqlClient.SqlDataAdapter(sqlCommand, scFacility);
dsFacility = new System.Data.DataSet();

// Fill the dataset
sdaFacility.Fill(dsFacility, "Facility");
}
catch (System.Exception ex)
{
// Handle the exception, log it, etc.
...

// Throw the exception to be handled higher up the call
stack
throw;
}
finally
{
// Close the connection to the database
scFacility.Close();
scFacility.Dispose();
}

// Return the information
return dsFacility;
}
}

I'm not even sure of what to ask at this point. But, is this a good way of
handling things? Is there a better way?

It just doesn't seem to be working as expected. For example, if an
exception is thrown in the data access class, it is caught and logged. Then
the code returns to the business rules class in the exception handler, but
now the exception is undefined. And then trying to throw this undefined
exception seems to be throwing an exceptions itself (I think. It gets kind
of confusing at this point.). Finally, we get back to the web application
and catch some exception, which may or may not even be the right thing.

It's very convoluted. That's why I'm thinking that there's got to be a
better way.

Hope this all makes sense. Any help/advice is greatly appreciated. Please
let me know if I can provide other information.

Regards.


May 3 '06 #1
1 3006
1. On thing that I would suggest is that you should almost never trap
anthing but an ApplicationException or an exception that inherits from it.
Change your System.Exceptions.

2. If you are only logging the exception in your lower level classes, why
not just allow it to bubble up?

3. Trap all exceptions in your Global exception handler at the top of the
chain. For this do use the System.Exception that you have.

4. Make sure you also log inner exceptions!

--

Andrew Robinson
http://blog.binaryocean.com
"metsys" <no****@company.com> wrote in message
news:e%****************@TK2MSFTNGP02.phx.gbl...
We have an ASP.NET 2.0 (C#) application that is divided into multiple
layers. The multiple layers come from having a web project and 2
different
class library projects in the same solution.

I'm having difficulties figuring out the best way to handle (catch)
exceptions in the different layers and then propagating those errors back
up
through the call stack to ultimately display something to the end-user.
Note this is an intranet application to be used in-house by company
employees; it will not be a public Internet application.

See the code snippets below for an example of how we're handling things
now.

ASP.NET Application
Web.config
<system.web>
<customErrors defaultRedirect="~/ErrorPages/ExceptionHandler.aspx"
mode="On" />
</system.web>
Global.asax
void Application_Error(Object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
// Formulate a message to write to the event log
...
}
Code-behind of .aspx page
System.Data.DataSet dsFacility = null;

try
{
// Do some other processing here
...

// Get the facility info from the database
dsFacility = BusinessRules.Facility.GetFacility();

// Do some other processing here
...
}
catch (System.Exception ex)
{
// Handle the exception, log it
...

// Throw the exception to be handled higher up the call stack
throw;
}

Business Rules class (this is a separate class library project)
public static class Facility
{
public static System.Data.DataSet GetFacility()
{
// Get the facility information
System.Data.DataSet dsFacility = null;

try
{
// Do some other processing here
...

// Get the facility info from the database
dsFacility = DataAccess.Facility.GetFacility();

// Do some other processing here
...
}
catch (System.Exception ex)
{
// Handle the exception, log it, etc.
...

// Throw the exception to be handled higher up the call
stack
throw;
}

// Rows found, return the queried dataset
return dsFacility;
}
}

Data Access class (this is a separate class library project)
public static class Facility
{
public static System.Data.DataSet GetFacility()
{
// Define and initialize local variables
string sqlCommand = "";
System.Data.SqlClient.SqlConnection scFacility = null;
System.Data.SqlClient.SqlDataAdapter sdaFacility = null;
System.Data.DataSet dsFacility = null;

try
{
// Create and populate the local variables
sqlCommand = "SELECT * FROM Facility ORDER BY
FacilityName";
scFacility = new
System.Data.SqlClient.SqlConnection(DatabaseConfig uration.RegPerfectDbConnec
tionString);
sdaFacility = new
System.Data.SqlClient.SqlDataAdapter(sqlCommand, scFacility);
dsFacility = new System.Data.DataSet();

// Fill the dataset
sdaFacility.Fill(dsFacility, "Facility");
}
catch (System.Exception ex)
{
// Handle the exception, log it, etc.
...

// Throw the exception to be handled higher up the call
stack
throw;
}
finally
{
// Close the connection to the database
scFacility.Close();
scFacility.Dispose();
}

// Return the information
return dsFacility;
}
}

I'm not even sure of what to ask at this point. But, is this a good way
of
handling things? Is there a better way?

It just doesn't seem to be working as expected. For example, if an
exception is thrown in the data access class, it is caught and logged.
Then
the code returns to the business rules class in the exception handler, but
now the exception is undefined. And then trying to throw this undefined
exception seems to be throwing an exceptions itself (I think. It gets
kind
of confusing at this point.). Finally, we get back to the web application
and catch some exception, which may or may not even be the right thing.

It's very convoluted. That's why I'm thinking that there's got to be a
better way.

Hope this all makes sense. Any help/advice is greatly appreciated.
Please
let me know if I can provide other information.

Regards.

May 3 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Abelardo Vacca | last post by:
Hello, I am currently in the process of switching our application to a N-Tier model with .NET. One of the aspects we want ot get right from the start not to worry about it after is the...
2
by: tom | last post by:
Hi, I am developing a WinForm application and I am looking for a guide on where to place Exception Handling. My application is designed into three tiers UI, Business Objects, and Data Access...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
6
by: Chris Newcombe | last post by:
Please could someone on the VC++ 7.0 compiler team (note; not 7.1) tell me if this code is handled 'correctly' (i.e. as the original poster suggests) in all cases? ...
7
by: Andy | last post by:
Hi all, This is more of a design question. I'm building an n-tier application, and the question revolves around the buisness layer telling the application that a value is not appropriate for...
2
by: Rajeev Soni | last post by:
Hi, Considering the scenario for handling exceptions in Web Application where we have Presentation layer, Business layer and Data Access layer; if there any exception is occurred in DAL, what is...
11
by: chopsnsauce | last post by:
Here's the example: Dim frm As New FORM1 Try frm.show Catch ex As Exception msgbox ex.message
1
by: metsys | last post by:
We have an ASP.NET 2.0 (C#) application that is divided into multiple layers. The multiple layers come from having a web project and 2 different class library projects in the same solution. I'm...
14
by: jehugaleahsa | last post by:
Hello: As an avid reader of C++ books, I know a lot of programmers out there are confronted with the challenge of exception safety. For those who don't know what it is, it is writing code in...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.