建议制定一个可重复使用try / catch块在C#中? [英] Suggestions for making a reusable try/catch block in C#?

查看:224
本文介绍了建议制定一个可重复使用try / catch块在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有大约20-一些它的方法的类。每个人做了一些Web服务消息处理。我不得不做出改变它,并认识到每一个方法都有它周围的完全相同的try / catch语句:

I have a class that has about 20-some methods in it. Each one does some web service message processing. I just had to make a change to it, and realized that every one of these methods has the exact same try/catch around it:

        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块?任何其他的想法?

My question is; instead of having this exact same try/catch block in every method, is there a way to make it common? My thoughts were that .NET has stuff like TransactionScope that somehow detects if an exception occurs when leaving that block. Is there I was I can leverage something like that to make a common try/catch block? Any other ideas?

推荐答案

我会做这样的:

创建一个包含try / catch语句的方法,并通过了动作成,并执行试部内的行动:

Create a method that contains the try/catch and pass an Action into it and execute that action inside the try part:

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 显示,不需要变量动作在你的方法,你可以内联的,如果你认为这是更具可读性。

The two versions of SafeExecutor give you the possibility to handle methods with and without return types.
Method1b shows that you don't need the variable action in your methods, you can inline it, if you think that's more readable.

这篇关于建议制定一个可重复使用try / catch块在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆