catch exceptions

R

RedLars

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;
}
}
 
P

Pavel Minaev

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/micr...read/thread/1bee6895c558d903/11b326f3bd849fbb

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.
 
I

Ignacio Machin ( .NET/ C# MVP )

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top