Hi,
when I have a method in code behind thqt has try code block. I can capture the error and post a message to the user.
I am ok with that. But, when I move the method to Data Access Layer, I cannot post a message instead, I throw the error... is this right,
so the calling method should have another catch try block to capture the raised error and post the messagee .
I appreciate your feedback on best way to post an error created in DAL
here is some code
I have some methods in Data Access tier of which it has try catch block. I want to discuss the catch block
like this one. so, if the code catch an sql client error, in DAL,
try
{
// calling a method in Data Access Layer
}
catch (SqlException SqlEx)
{
string SqlError = SqlEx.Message.ToString();
showMsgBox(SqlError);
}
the code in DAL inside a method that generate the error
-------------------------------------------------------
catch (System.Data.SqlClient.SqlException ex)
{
throw ex;
}
Why is it a bad idea to throw your own exceptions? Well, if at that point you know that an error has occurred, then why not write the proper code to handle that error instead of passing a new Exception object to the catch block? Throwing your own
exceptions signifies some design flaws in the project.
bobwhite
Member
701 Points
667 Posts
how to catch and post an error generated in DAL to the user
Dec 22, 2012 04:21 PM|LINK
Hi,
when I have a method in code behind thqt has try code block. I can capture the error and post a message to the user.
I am ok with that. But, when I move the method to Data Access Layer, I cannot post a message instead, I throw the error... is this right,
so the calling method should have another catch try block to capture the raised error and post the messagee .
I appreciate your feedback on best way to post an error created in DAL
here is some code
I have some methods in Data Access tier of which it has try catch block. I want to discuss the catch block
like this one. so, if the code catch an sql client error, in DAL,
try
{
// calling a method in Data Access Layer
}
catch (SqlException SqlEx)
{
string SqlError = SqlEx.Message.ToString();
showMsgBox(SqlError);
}
the code in DAL inside a method that generate the error
-------------------------------------------------------
catch (System.Data.SqlClient.SqlException ex)
{
throw ex;
}
eric2820
Contributor
2777 Points
1161 Posts
Re: how to catch and post an error generated in DAL to the user
Dec 22, 2012 07:36 PM|LINK
If your going to catch exceptions, you should be able to handle them rather than just re-throw the exception.
At the very least you should be checking the exception for certain cases and reporting them directly.
http://www.my-msi.net/Admin
blog
If a post helps you, please mark it as Ansered, thank-you.
bobwhite
Member
701 Points
667 Posts
Re: how to catch and post an error generated in DAL to the user
Dec 27, 2012 04:58 PM|LINK
Hi Eric,
If I handled the exception in Business Layer or in Data Layer, how can I pass error message to the user in GUI tier?
Bob
eric2820
Contributor
2777 Points
1161 Posts
Re: how to catch and post an error generated in DAL to the user
Dec 27, 2012 05:00 PM|LINK
The 'old-school' method would be to return an error code that the application was aware of.
That is a good method to fall back to in the case where you can't come up with anything better.
http://www.my-msi.net/Admin
blog
If a post helps you, please mark it as Ansered, thank-you.
eric2820
Contributor
2777 Points
1161 Posts
Re: how to catch and post an error generated in DAL to the user
Dec 27, 2012 05:03 PM|LINK
BTW. this is the basis of my earlier reply:
Why is it a bad idea to throw your own exceptions? Well, if at that point you know that an error has occurred, then why not write the proper code to handle that error instead of passing a new Exception object to the catch block? Throwing your own exceptions signifies some design flaws in the project.
http://www.my-msi.net/Admin
blog
If a post helps you, please mark it as Ansered, thank-you.