在C#中制作可重用的try / catch块的建议?

时间:2011-05-18 14:28:59

标签: c# .net exception-handling try-catch

我有一个类,其中包含大约20个方法。每个人都做一些Web服务消息处理。我只需对其进行更改,并意识到这些方法中的每一个都具有完全相同的try / catch:

        try
        {
            /* *** actual processing specific to each method goes here *** */
        }
        catch (FaultException<CustomException> cfex)
        {
            // common stuff
        }
        catch (CustomException cfex)
        {
            // common stuff
        }
        catch (Exception ex)
        {
            // common stuff
        }
        finally
        {
            FinalizeServiceCall(wsBus, wsMessage, response, logProps);
        }

我的问题是;而不是在每个方法中都有这个完全相同的try / catch块,有没有办法让它变得常见?我的想法是.NET有像TransactionScope这样的东西,以某种方式检测离开该块时是否发生异常。有没有我可以利用这样的东西来制作一个常见的try / catch块?还有其他想法吗?

5 个答案:

答案 0 :(得分:46)

我会这样做:

创建一个包含try / catch的方法并将Action传递给它,并在try部分中执行该操作:

public void Method1()
{
    Action action = () =>
    {
        // actual processing of Method 1
    };
    SafeExecutor(action);
}

public void Method1b()
{
    SafeExecutor(() =>
    {
        // actual processing of Method 1
    });
}

public void Method2(int someParameter)
{
    Action action = () =>
    {
        // actual processing of Method 2 with supplied parameter
        if(someParameter == 1)
        ...
    };
    SafeExecutor(action);
}

public int Method3(int someParameter)
{
    Func<int> action = () =>
    {
        // actual processing of Method 3 with supplied parameter
        if(someParameter == 1)
            return 10;
        return 0;
    };
    return SafeExecutor(action);
}

private void SafeExecutor(Action action)
{
    SafeExecutor(() => { action(); return 0; });
}

private T SafeExecutor<T>(Func<T> action)
{
    try
    {
        return action();
    }
    catch (FaultException<CustomException> cfex)
    {
        // common stuff
    }
    catch (CustomException cfex)
    {
        // common stuff
    }
    catch (Exception ex)
    {
        // common stuff
    }
    finally
    {
        FinalizeServiceCall(wsBus, wsMessage, response, logProps);
    }

    return default(T);
}

SafeExecutor的两个版本使您可以处理带有和不带返回类型的方法 Method1b表示您的方法中不需要变量action,如果您认为它更具可读性,则可以内联它。

答案 1 :(得分:7)

有很多方法可以轻松完成 - 首先对我来说,我已经开始使用AOP来捕捉异常

这会有效地转换你的代码

try
        {
            /* *** actual processing specific to each method goes here *** */
        }
        catch (FaultException<CustomException> cfex)
        {
            // common stuff
        }
        catch (CustomException cfex)
        {
            // common stuff
        }
        catch (Exception ex)
        {
            // common stuff
        }
        finally
        {
            FinalizeServiceCall(wsBus, wsMessage, response, logProps);
        }

类似

[HandleException( Exception , FaultException<CustomException>, 
                      "Error Getting Details" )]
    public MYType GetDetails( string parameter )
    {
        //.... call to service
    }

使用Postsharp - details here

Mark Rendle how to catch exceptions in a Functional Programming way上有{{3}}的博客帖子 - 我还没有试过这个帖子

答案 2 :(得分:5)

您已确定跨领域问题。您可以采用面向方面编程(AOP)方法来解决此问题。这可以在运行时通过使用位于类前面的代理或在编译期间使用修改已编译代码的AOP工具来执行。

过去我曾使用Castle Dynamic Proxy来执行此操作(在运行时)。或者,您可以使用其他AOP框架之一,例如PostSharp

答案 3 :(得分:1)

如果参数相同或接近相同,您始终可以传入委托。如果它们不是你可以通过反射调用代码并使用'object []'参数传递给调用。

答案 4 :(得分:0)

您可以做的是在一个方法中编写上面的代码,该方法将Action或Func作为参数来确定应该在throw块中调用的方法及其参数。

因此,如果您在throw块中调用M(1, "string"),它将变为DoStuff(M, 1, "string")

DoStuff看起来像

void DoStuff<T1, T2, TResult>(Func<T1, T2, TResult> myMethod, T1 arg1, T2 arg2)
{
        try
        {
            myMethod(arg1, arg2)
        }
        catch (FaultException<CustomException> cfex)
        {
            // common stuff
        }
        catch (CustomException cfex)
        {
            // common stuff
        }
        catch (Exception ex)
        {
            // common stuff
        }
        finally
        {
            FinalizeServiceCall(wsBus, wsMessage, response, logProps);
        }
}
相关问题