How to avoid exceptions catches copy-paste in .NET

Posted by Budda on Stack Overflow See other posts from Stack Overflow or by Budda
Published on 2010-06-17T18:28:53Z Indexed on 2010/06/17 18:33 UTC
Read the original article Hit count: 221

Filed under:
|
|
|

Working with .NET framework I have a service with a set of methods that can generates several types of exceptions: MyException2, MyExc1, Exception... To provide proper work for all methods, each of them contains following sections:

void Method1(...)
{
    try
    {
        ... required functionality
    }
    catch(MyException2 exc)
    {
        ... process exception of MyException2 type
    }
    catch(MyExc1 exc)
    {
        ... process exception of MyExc1 type
    }
    catch(Exception exc)
    {
        ... process exception of Exception type
    }
    ... process and return result if necessary
}

It is very boring to have exactly same stuff in EACH service method with exactly same exceptions processing functionality...

Is there any possibility to "group" these catch-sections and use only one line (something similar to C++ macros)? Probably something new in .NET 4.0 is related to this topic?

Thanks.

P.S. Any thoughts are welcome.

© Stack Overflow or respective owner

Related posts about .NET

Related posts about exception