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

Home Posts Topics Members FAQ

Error Handling - Best Practices

Hello -

I read an interesting article on the web wherein the author states he
doesn't handle too many errors at page level, but handles them at the
application level. He further goes on to show how error logging can be
incorporated using a Sql Server log with an additional text log in case Sql
Server was down.

My inclination after reading the article is to use Try-Catch-Finally as
little as possible (tends to slow things if you have too many?) and rely on
the application level.

Is this considered a "best practice"? If not, why?

Any help will be appreciated!
--
Sandy
Nov 19 '05 #1
4 1972
> I read an interesting article on the web wherein the author states he
doesn't handle too many errors at page level, but handles them at the
application level. He further goes on to show how error logging can
be incorporated using a Sql Server log with an additional text log in
case Sql Server was down.
Yes, the Application_Error event handler in global.asax is a great place
to log unhandled exceptions. Any exception that isn't specifically caught
in your application somewhere will end up at Application_Error. This allows
you to be proactive about finding latent bugs and other bad things in your
application.
My inclination after reading the article is to use Try-Catch-Finally
as little as possible (tends to slow things if you have too many?) and
rely on the application level.
try/catch blocks are not slow when there is no exception. It's only when
an exception happens that they tend to run slower because of the stack unwinding
that has to be done. But if it only happens in an exception case, then this
isn't a problem. Also, if the operation failed, then it's not a problem to
take a bit of extra time to be able to recover from it (again, assuming this
isn't the expected path of frequent execution). finally blocks really don't
execute any faster or slower in either situation.
Is this considered a "best practice"? If not, why?


Personally, I only handle exceptions (via catch) when I know that I can write
code to recover from the exception. This isn't common, actually... How do
you, in code, recover from a database connectivity exception? Things like
that are very hard to handle. Database timeouts? Well, that's a bit harder...
perhaps it's possible to resubmit the SQL, but then maybe it's not (especially
if you're in a larger transaction). So really, I just judge it case by case.

I always use finally blocks when I need a guarentee that a certain code block
execute (primarily to do resource cleanup like close DB connections).

-Brock
DevelopMentor
http://staff.develop.com/ballen


Nov 19 '05 #2
Ye
There is a difference between error handling and error reporting.

If you are talking about error handling I don't think there is a better way
other than using try-catch. When saying that try-catch is slow, one is
usually comparing it to using an if statement to catch the condition that
will cause the error before an error is thrown. It is good practice to try to
catch an error-causing condition instead of catching the exception.

If you are talking about error reporting, I personally like ELMAH, written
by Atif Aziz, the best. Scott Mitchell wrote an excellent article about it
here
http://msdn.microsoft.com/library/de...html/elmah.asp.
It's easy to setup and REALLY reusable. It is decoupled from your application
and no re-compilation of your website is necessary.

Hope this helps.
Cheng Yuan Yap Ye
http://kuantanzai.blogspot.com
"Brock Allen" wrote:
I read an interesting article on the web wherein the author states he
doesn't handle too many errors at page level, but handles them at the
application level. He further goes on to show how error logging can
be incorporated using a Sql Server log with an additional text log in
case Sql Server was down.


Yes, the Application_Error event handler in global.asax is a great place
to log unhandled exceptions. Any exception that isn't specifically caught
in your application somewhere will end up at Application_Error. This allows
you to be proactive about finding latent bugs and other bad things in your
application.
My inclination after reading the article is to use Try-Catch-Finally
as little as possible (tends to slow things if you have too many?) and
rely on the application level.


try/catch blocks are not slow when there is no exception. It's only when
an exception happens that they tend to run slower because of the stack unwinding
that has to be done. But if it only happens in an exception case, then this
isn't a problem. Also, if the operation failed, then it's not a problem to
take a bit of extra time to be able to recover from it (again, assuming this
isn't the expected path of frequent execution). finally blocks really don't
execute any faster or slower in either situation.
Is this considered a "best practice"? If not, why?


Personally, I only handle exceptions (via catch) when I know that I can write
code to recover from the exception. This isn't common, actually... How do
you, in code, recover from a database connectivity exception? Things like
that are very hard to handle. Database timeouts? Well, that's a bit harder...
perhaps it's possible to resubmit the SQL, but then maybe it's not (especially
if you're in a larger transaction). So really, I just judge it case by case.

I always use finally blocks when I need a guarentee that a certain code block
execute (primarily to do resource cleanup like close DB connections).

-Brock
DevelopMentor
http://staff.develop.com/ballen


Nov 19 '05 #3
Thanks, Brock!

"Brock Allen" wrote:
I read an interesting article on the web wherein the author states he
doesn't handle too many errors at page level, but handles them at the
application level. He further goes on to show how error logging can
be incorporated using a Sql Server log with an additional text log in
case Sql Server was down.


Yes, the Application_Error event handler in global.asax is a great place
to log unhandled exceptions. Any exception that isn't specifically caught
in your application somewhere will end up at Application_Error. This allows
you to be proactive about finding latent bugs and other bad things in your
application.
My inclination after reading the article is to use Try-Catch-Finally
as little as possible (tends to slow things if you have too many?) and
rely on the application level.


try/catch blocks are not slow when there is no exception. It's only when
an exception happens that they tend to run slower because of the stack unwinding
that has to be done. But if it only happens in an exception case, then this
isn't a problem. Also, if the operation failed, then it's not a problem to
take a bit of extra time to be able to recover from it (again, assuming this
isn't the expected path of frequent execution). finally blocks really don't
execute any faster or slower in either situation.
Is this considered a "best practice"? If not, why?


Personally, I only handle exceptions (via catch) when I know that I can write
code to recover from the exception. This isn't common, actually... How do
you, in code, recover from a database connectivity exception? Things like
that are very hard to handle. Database timeouts? Well, that's a bit harder...
perhaps it's possible to resubmit the SQL, but then maybe it's not (especially
if you're in a larger transaction). So really, I just judge it case by case.

I always use finally blocks when I need a guarentee that a certain code block
execute (primarily to do resource cleanup like close DB connections).

-Brock
DevelopMentor
http://staff.develop.com/ballen


Nov 19 '05 #4
Thanks, Ye. Scott Mitchell is one of my favorites.

Sandy

"Ye" wrote:
There is a difference between error handling and error reporting.

If you are talking about error handling I don't think there is a better way
other than using try-catch. When saying that try-catch is slow, one is
usually comparing it to using an if statement to catch the condition that
will cause the error before an error is thrown. It is good practice to try to
catch an error-causing condition instead of catching the exception.

If you are talking about error reporting, I personally like ELMAH, written
by Atif Aziz, the best. Scott Mitchell wrote an excellent article about it
here
http://msdn.microsoft.com/library/de...html/elmah.asp.
It's easy to setup and REALLY reusable. It is decoupled from your application
and no re-compilation of your website is necessary.

Hope this helps.
Cheng Yuan Yap Ye
http://kuantanzai.blogspot.com
"Brock Allen" wrote:
I read an interesting article on the web wherein the author states he
doesn't handle too many errors at page level, but handles them at the
application level. He further goes on to show how error logging can
be incorporated using a Sql Server log with an additional text log in
case Sql Server was down.


Yes, the Application_Error event handler in global.asax is a great place
to log unhandled exceptions. Any exception that isn't specifically caught
in your application somewhere will end up at Application_Error. This allows
you to be proactive about finding latent bugs and other bad things in your
application.
My inclination after reading the article is to use Try-Catch-Finally
as little as possible (tends to slow things if you have too many?) and
rely on the application level.


try/catch blocks are not slow when there is no exception. It's only when
an exception happens that they tend to run slower because of the stack unwinding
that has to be done. But if it only happens in an exception case, then this
isn't a problem. Also, if the operation failed, then it's not a problem to
take a bit of extra time to be able to recover from it (again, assuming this
isn't the expected path of frequent execution). finally blocks really don't
execute any faster or slower in either situation.
Is this considered a "best practice"? If not, why?


Personally, I only handle exceptions (via catch) when I know that I can write
code to recover from the exception. This isn't common, actually... How do
you, in code, recover from a database connectivity exception? Things like
that are very hard to handle. Database timeouts? Well, that's a bit harder...
perhaps it's possible to resubmit the SQL, but then maybe it's not (especially
if you're in a larger transaction). So really, I just judge it case by case.

I always use finally blocks when I need a guarentee that a certain code block
execute (primarily to do resource cleanup like close DB connections).

-Brock
DevelopMentor
http://staff.develop.com/ballen


Nov 19 '05 #5

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

Similar topics

2
by: aaj | last post by:
Hi all I have an automated application, that runs in the middle of the night. If certain 'non system' errors occur (things like malformed files, missing files etc..), I send an automatic Email...
12
by: Christian Christmann | last post by:
Hi, assert and error handling can be used for similar purposes. When should one use assert instead of try/catch and in which cases the error handling is preferable? I've read somewhere that...
2
by: Steve Jorgensen | last post by:
When writing VB or VBA code that works with databases or other external libraries that cannot be trusted to automatically do the right thing when references to their objects are arbitrarily...
4
by: aaj | last post by:
Hi all I have an automated application, that runs in the middle of the night. If certain 'non system' errors occur (things like malformed files, missing files etc..), I send an automatic Email...
0
by: Benny Ng | last post by:
Hi,All, When i deploy Enterprise library with my application ,i used XCOPY to deploy it into my test server. But when application runs, shown some error related registry. (But actually I haven't...
4
by: James Radke | last post by:
Hello, I am looking for guidance on best practices to incorporate effective and complete error handling in an application written in VB.NET. If I have the following function in a class module...
1
by: Jon | last post by:
Can someone point me to a best practices for exception handling in .Net 2? We have a method that we like for ASP.Net apps on .Net 1.1, but are just moving to 2.0 with winforms apps. Is it still...
1
by: maciek | last post by:
Hi, I was wondering if anyone could suggest me a book/article/tutorial on Exception Handling in multi tier Windows Apps. What are the best practices/ways to implement EH in multi tier...
0
by: joshfink | last post by:
Hey guys, I am writing an application where I want to follow the best practices on error handling. This is what I have: I created an enum for various issues that could happen within the...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.