473,394 Members | 1,219 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Exception handling best practices

Is this good style? I always have to catch the generic System.Exception
because our system supports nested transactions that is BeginTransaction
increases the level, Commit decreases it, rollback sets it to zero, and only
if the level reaches zero the commit/rollback statement is sended to the
database.

try
{
BeginTransaction();
// stuff
Commit();
}
catch (SpecialException e)
{
Rollback();
throw new ApplicationException(e.Message);
}
catch (Exception)
{
Rollback();
throw;
}
Jul 21 '05 #1
2 3469
Couple of points about:
catch (SpecialException e) ...
The statement:
throw new ApplicationException(e.Message);

is usually not a good practice. You should instead use something like this:
throw new ApplicationException("Some context message that provides
additional meaning to the user.",e);

The original statement would work but usually is not a good practice - all
you are doing is changing the exception type, but you are losing the
original stack trace and are not providing any additional information at
all.

One justification for the original statement is if there is security
sensitive information in the original exception that you do not want exposed
to a higher level component and you wish to hide the data.

An additional reason for transforming the exception type is if the exception
that is caught (SpecialException), is a custom exception that is defined in
an assembly that is not available to modules that have called into your code
(e.g. they are calling across a boundary, either appdomain, machine, etc).
In this case you actually do need to discard the original type and replace
it with a different type. This is because if the calling module does not
have access to the assembly then it will not be able to deserialize the
exception object and that will itself cause another exception to be thrown,
masking the original exception.

In either case I would preserve the original exception message and add
additional context information to aid users in figuring out why it didn't
work.

Another point is that use of the type ApplicationException is being
discouraged as it does not really add much value. There is a ongoing debate
about this and it has not yet been resolved in any camp's favor, but I
personally never use it anymore - I either use a custom exception (rare), or
use the same exception type that was caught (i.e. clone the type), or
sometimes just use Exception.
Couple of points about
catch (Exception)
{
Rollback();
throw;
}

Again, this works, but due to a bug in the current version of the runtime it
will lose the original stack information and replace it with a stack that
begins where the throw statement occurs. This is supposed to be fixed in
Whidbey. It also does not add any context information.

I usually prefer to catch-wrap-throw rather then simply rethrow the original
exception. In this case I would probably use something like...

catch (SpecialException e)
{
Rollback();
throw new Exception("Unable to complete.",e);
}
catch(Exception ex)
{
Rollback();
throw new Exception("I fell down and can't get up.",ex);
}

I always prefer to add information in order to make life as easy as possible
for the users - cryptic error messages should be avoided at all costs.
"cody" <de********@gmx.de> wrote in message
news:un**************@TK2MSFTNGP10.phx.gbl...
Is this good style? I always have to catch the generic System.Exception
because our system supports nested transactions that is BeginTransaction
increases the level, Commit decreases it, rollback sets it to zero, and
only
if the level reaches zero the commit/rollback statement is sended to the
database.

try
{
BeginTransaction();
// stuff
Commit();
}
catch (SpecialException e)
{
Rollback();
throw new ApplicationException(e.Message);
}
catch (Exception)
{
Rollback();
throw;
}

Jul 21 '05 #2
> One justification for the original statement is if there is security
sensitive information in the original exception that you do not want
exposed to a higher level component and you wish to hide the data.

An additional reason for transforming the exception type is if the
exception that is caught (SpecialException), is a custom exception that is
defined in an assembly that is not available to modules that have called
into your code
Good points, I never thought about that.
Another point is that use of the type ApplicationException is being
discouraged as it does not really add much value.
You are right but we started our project with using ApplicationException as
convention so we have to finish it like that because now most of our modules
expect now our components to throw only ApplicationException and its
descendants :(
catch(Exception ex)
{
Rollback();
throw new Exception("I fell down and can't get up.",ex);
}

I always prefer to add information in order to make life as easy as
possible for the users - cryptic error messages should be avoided at all
costs.


The catch(Exception) was thought only for the theoretically impossible case
but I think in that case it is better that the messagebox displays the user
the original exception message ("cannot connect to database") than my own
text ("something really fouled up during creation of customer").
Jul 21 '05 #3

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

Similar topics

6
by: Daniel Wilson | last post by:
I am having exception-handling and stability problems with .NET. I will have a block of managed code inside try...catch and will still get a generic ..NET exception box that will tell me which...
6
by: ORC | last post by:
I'm looking for a good article that explains how exception handling should be implemented in an application. It's not only the tecnical implementation (try-catch-final thing) but a good general...
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...
1
by: aftab Alam | last post by:
Hi All, I have am working on C# 2.0 for a project and need to know the best practices for exception handling. At the moment I am doing it as Follows. I have a Manager class that is supposed...
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: David Herbst | last post by:
Enterprise Library Jan 2006 with Visual Studio 2005 on Windows 2000 Server sp4. My custom exception formatter fails with a "Unable to handle exception: 'LoggingExceptionHandler'." exception. ...
10
by: tony | last post by:
Hello!! As you know every user defined exception must be derived from class Exception. Now to my question if I write catch then every exception will be caught. If I instead write...
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...
24
by: Earl | last post by:
I have all of my data operations in a separate library, so I'm looking for what might be termed "best practices" on a return type from those classes. For example, let's say I send an update from...
0
by: =?Utf-8?B?UG9sbHkgQW5uYQ==?= | last post by:
Hi, I have previously used EL v 3.1 Exception Handling application block successfully. I thought I would now try to do the same with EL v 4.0. My first experiment was to replace an exception....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.