Answered by:
simple error handling in three tiers

Question
-
User-597214666 posted
Hi there,
I have Presentation Layer. Business Logic Layer and a Data Later.
In my Data Layer I'm wrapping my code that open and closes the connection to the DB. Like this:
public static DataSet ExcecuteDataset(string spCommandText) { DataSet ds = new DataSet(); SqlDataAdapter scom = new SqlDataAdapter(spCommandText, connection); scom.SelectCommand.CommandType = CommandType.Text; try { scom.Fill(ds); scom.SelectCommand.Connection.Close(); } catch (Exception ex) { } return ds; }
If I was using my presentation layer I could set the exception to a label. But now when I catch an exception, what do I do with it? I can't return it as string because it's expecting a type DataSet. Do I just use 'throw'?
Thursday, December 17, 2009 9:14 AM
Answers
-
User-627724879 posted
You should bubble it up to the user in a digestible format. A typical practice is to throw a new exception, preferabbly one that is custom to your application so you can indicate some things to dictact how it is consumed in the UI. Normally I would include some sort of try catch routine in a custom page class that all my pages inherit from so I can display the error message to the user in a controlled fashion.
I mean if it is somethere where they provided a bad peice of data, maybe you higlight the input field accordingly rather than a generic error message for them to read. It all really depends on what you are comfortable with doing.
Also remember the Page class has an OnError event handler that you can intercept to catch unhandled exceptions, but I think you are doing the better job here doing at the point of action instead of the UI.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, December 17, 2009 9:32 AM -
User904860486 posted
Yes use throw; in DAL-Which will throw the error it catches in catch bolock
You can again use throw; in BLL which will get the exception arised in BLL as well as the exception that was thrown in DAL
Finally use try..catch block in PL and in catch block..Do the handling method that you want to do....
This catch block will catch all the Exception arised in DAL,BLL as well as PL
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, December 17, 2009 9:35 AM -
User-1067358373 posted
If you are not handling the error in catch block, there is no point in simply catching the exception and throwing it.
Let the error bubble up and catch in the application_error event in global.asax file. Application_error event is the place to catch
all unhandled exceptions. I would strongly suggest you to implement this event handler.
using System.Diagnostics;
protected void Application_Error(object sender, EventArgs e)
{
Exception objErr = Server.GetLastError().GetBaseException();
string err = "Error Caught in Application_Error event\n" +
"Error in: " + Request.Url.ToString() +
"\nError Message:" + objErr.Message.ToString()+
"\nStack Trace:" + objErr.StackTrace.ToString();
EventLog.WriteEntry("Sample_WebApp",err,EventLogEntryType.Error);
Server.ClearError();
//additional actions...
}
http://support.microsoft.com/kb/306355- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, December 24, 2009 12:06 PM
All replies
-
User-627724879 posted
You should bubble it up to the user in a digestible format. A typical practice is to throw a new exception, preferabbly one that is custom to your application so you can indicate some things to dictact how it is consumed in the UI. Normally I would include some sort of try catch routine in a custom page class that all my pages inherit from so I can display the error message to the user in a controlled fashion.
I mean if it is somethere where they provided a bad peice of data, maybe you higlight the input field accordingly rather than a generic error message for them to read. It all really depends on what you are comfortable with doing.
Also remember the Page class has an OnError event handler that you can intercept to catch unhandled exceptions, but I think you are doing the better job here doing at the point of action instead of the UI.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, December 17, 2009 9:32 AM -
User904860486 posted
Yes use throw; in DAL-Which will throw the error it catches in catch bolock
You can again use throw; in BLL which will get the exception arised in BLL as well as the exception that was thrown in DAL
Finally use try..catch block in PL and in catch block..Do the handling method that you want to do....
This catch block will catch all the Exception arised in DAL,BLL as well as PL
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, December 17, 2009 9:35 AM -
User-1455009591 posted
Hi,
best practice to propagate an exception thru three layer application is as follows:
1.)in data layer throw SqlException
2.)catch it in bussines layer, log it and wrap it into user-defined exception
3.)catch the user-defined exception in presentation layer and display it to user in simple, understandable form
Thursday, December 17, 2009 9:42 AM -
User-597214666 posted
Hi docluv and Zleo,
Thanks for your replies. Would you mind giving me a simple example of what you mean. I'm not sure how to 'bubble' the error up from the Datalayer...
But thanks I'll work on it in the meantime :D
Thursday, December 17, 2009 10:05 AM -
User-597214666 posted
Ok, in my DataLayer Catch block I'm using throw. In my BLL using throw in that catch block too. And in my presentation later again I have a tr catch block with error handling. Seems a bit like overkill however it's working now. If you have better suggestions please let me know. Thanks for the replies again.
Presentation Layer:
try { MainDataLayer.UserVO user = new MainDataLayer.UserVO(); user = MainBusinessLayer.UserBL.GetUserById(UserId); UserId = Request.QueryString["UserId"].ToString(); tbDescription.Text = UserId; user = MainBusinessLayer.UserBL.GetUserById(UserId); tbUsername.Text = user.Username; tbPassword.Text = user.Password; tbFname.Text = user.Firstname; tbSurname.Text = user.Lastname; tbDescription.Text = user.Description; } catch (Exception ex) { throw; }
BLL:
try { DataSet UserDS = MainDataLayer.UserDL.GetUserById(userId); foreach (DataRow dr in UserDS.Tables[0].Rows) { userVO.Username = dr["Username"].ToString(); userVO.Password = dr["Password"].ToString(); userVO.Firstname = dr["Fname"].ToString(); userVO.Lastname = dr["Lname"].ToString(); userVO.Description = dr["Description"].ToString(); } } catch (Exception ex) { throw; }
DLL:
public static DataSet ExcecuteDataset(string spCommandText) { DataSet ds = new DataSet(); SqlDataAdapter scom = new SqlDataAdapter(spCommandText, connection); scom.SelectCommand.CommandType = CommandType.Text; try { scom.Fill(ds); scom.SelectCommand.Connection.Close(); } catch (SqlException ex) { throw; } return ds; }
Thursday, December 17, 2009 10:30 AM -
User-1067358373 posted
If you are not handling the error in catch block, there is no point in simply catching the exception and throwing it.
Let the error bubble up and catch in the application_error event in global.asax file. Application_error event is the place to catch
all unhandled exceptions. I would strongly suggest you to implement this event handler.
using System.Diagnostics;
protected void Application_Error(object sender, EventArgs e)
{
Exception objErr = Server.GetLastError().GetBaseException();
string err = "Error Caught in Application_Error event\n" +
"Error in: " + Request.Url.ToString() +
"\nError Message:" + objErr.Message.ToString()+
"\nStack Trace:" + objErr.StackTrace.ToString();
EventLog.WriteEntry("Sample_WebApp",err,EventLogEntryType.Error);
Server.ClearError();
//additional actions...
}
http://support.microsoft.com/kb/306355- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, December 24, 2009 12:06 PM -
User-597214666 posted
dotnetkode
Thank you for your reply. It was exactly what i was looking for and more. I will definitely integrate this into my applications in the future.
Thursday, December 24, 2009 2:56 PM