PC Review


Reply
Thread Tools Rate Thread

catch exceptions

 
 
RedLars
Guest
Posts: n/a
 
      29th Apr 2009
Say I have the following structure of code in three different
locations in a project. There are actually 8 different exceptions,
only showed 4 for brevity. In the first location all 8 exceptions are
relevant but in the two other locations only 6 exceptions are
relevant. The handling of each exception would be the same for all 3
locations.

try
{
// do stuff
}
catch (MyExceptionX1 x1)
{
// do logging
// do action
}
catch (MyExceptionX2 x2)
{
// do logging
// do action
}
catch (MyExceptionX3 x3)
{
// do logging
// do action
}
catch (MyExceptionX4 x4)
{
// log
// do action
}

To reduce duplicate code would something like this be sensible;

try
{
// do stuff
}
catch (Exception e)
{
HandleExceptions(e);
}

public static MyExceptionClass
{
public static void HandleExceptions(Exception e)
{
MyExceptionX1 x1 = e is MyExceptionX1;
if (x1 != null)
{
// log
// do action
return
}

MyExceptionX2 x2 = e is MyExceptionX2;
if (x2 != null)
{
// log
// do action
return
}

// no match
throw e;
}
}
 
Reply With Quote
 
 
 
 
Pavel Minaev
Guest
Posts: n/a
 
      30th Apr 2009
On Apr 29, 7:04*am, RedLars <Liverpool1...@gmail.com> wrote:
> Say I have the following structure of code in three different
> locations in a project. There are actually 8 different exceptions,
> only showed 4 for brevity. In the first location all 8 exceptions are
> relevant but in the two other locations only 6 exceptions are
> relevant. The handling of each exception would be the same for all 3
> locations.
>
> try
> {
> * // do stuff}
>
> catch (MyExceptionX1 x1)
> {
> * // do logging
> * // do action}
>
> catch (MyExceptionX2 x2)
> {
> * // do logging
> * // do action}
>
> catch (MyExceptionX3 x3)
> {
> * // do logging
> * // do action}
>
> catch (MyExceptionX4 x4)
> {
> * // log
> * // do action
>
> }
>
> To reduce duplicate code would something like this be sensible;
>
> try
> {
> * // do stuff}
>
> catch (Exception e)
> {
> * * * * HandleExceptions(e);
>
> }
>
> public static MyExceptionClass
> {
> public static void HandleExceptions(Exception e)
> {
> * * * * MyExceptionX1 x1 = e is MyExceptionX1;
> * * * * if (x1 != null)
> * * * * {
> * * * * * * * * // log
> * * * * * * * * // do action
> * * * * * * * * return
> * * * * }
>
> * * * * MyExceptionX2 x2 = e is MyExceptionX2;
> * * * * if (x2 != null)
> * * * * {
> * * * * * * * * // log
> * * * * * * * * // do action
> * * * * * * * * return
> * * * * }
>
> * * * * // no match
> * * * * throw e;
> }
> }


Have a look at this thread on a neighboring newsgroup, where a very
similar question was asked. Pretty much all answers there are
applicable to your case as well:

http://groups.google.com/group/micro...b326f3bd849fbb

Note that the code there in VB, and the C# version is more terse
because you can just use anonymous delegates / lambdas.

Something that's probably worth re-iterating: rethrowing exceptions is
not so good in general, but if you do so, at least use "throw;" rather
than "throw ex;", because the former preserves the original stack
trace of the exception.
 
Reply With Quote
 
Ignacio Machin ( .NET/ C# MVP )
Guest
Posts: n/a
 
      30th Apr 2009
On Apr 29, 10:04*am, RedLars <Liverpool1...@gmail.com> wrote:
> Say I have the following structure of code in three different
> locations in a project. There are actually 8 different exceptions,
> only showed 4 for brevity. In the first location all 8 exceptions are
> relevant but in the two other locations only 6 exceptions are
> relevant. The handling of each exception would be the same for all 3
> locations.


It should work, if the handling of the exception is to be the same in
all places.
But beware, when you say that only 6 exceptions are relevant, are you
referring that you will handle only those 6 or than only those 6 can
be thrown? if the latter your code will work simply because the other
two will not be thrown, in the other case y ou have a problem cause
you need the exception to propagate
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to catch exceptions? JackPot Microsoft ASP .NET 7 6th Aug 2008 11:50 AM
Sample program: Try / Catch exceptions user defined exceptions derived from System.Exception raylopez99 Microsoft C# .NET 2 23rd Sep 2007 10:47 AM
Catch block is failing to catch exceptions when not run from MSDev CodeSlayer Microsoft C# .NET 2 16th Feb 2006 06:42 PM
What Exceptions to catch Steven Blair Microsoft C# .NET 11 15th Apr 2004 11:40 AM
Is there anyway to catch both exceptions in the following example? Hasani Microsoft C# .NET 4 17th Sep 2003 07:42 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:01 PM.