473,657 Members | 2,523 Online
Bytes | Software Development & Data Engineering Community
+ 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 1988
> 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_Err or 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_Err or. 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_Err or 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_Err or. 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_Err or 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_Err or. 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_Err or 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_Err or. 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
2033
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 and write a record to the database. This is handled in a class. When these errors occur, once Emailed and written I want to just end the App, simple as that.
12
6667
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 assert could be used to start an interactive debugger automatically. How do I realize that on a Linux machine using gcc?
2
3010
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 released, some thought must be put into how to make sure the objects will all be closed and released in the correct order, even in the result of an error. This requirement can make our code really ugly, even following the best of commonly known best...
4
2521
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 and write a record to the database. This is handled in a class. When these errors occur, once Emailed and written I want to just end the App, simple as that.
0
2629
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 any method or component to wrote registry) After I used "InstallUtil" to registry Enterprise Library (those DLLs) to the deployment server, the application runs smoothly. Now I wonder why i need to registry those Enterprise Library DLLs into
4
7608
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 (note that this class module represents the business layer of code NOT the gui layer): Public Function Test(ByVal Parm1 As Integer, ByVal Parm2 As Integer) As SqlDataReader ' Declare the SQL data layer class Dim oSQL As New...
1
258
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 recommended to use the addHandler method in each form for ApplicationThreadExceptions and then just handle everything else using try/catch blocks and rethrow if the exception can't be handled? Is it recommended to let all exceptions bubble up...
1
2505
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 enviroment. I read some MS Best Practices articles on MSDN. It helped a little, but considering how unexperienced programmer I am, I think I'd need a good sample code and explanation I could follow to fully understand the issue. Thanks in advance.
0
1243
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 DAL... INSERTFAILED, INSERTSUCCESSFUL etc... I am setting this enum to a property of the dal for the object to access after the dal is done with whatever method I call... get, getAll, update, insert, delete, etc... The object can then access...
0
8392
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8823
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8603
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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 we have to send another system
2
1944
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.