错误处理的设计模式

时间:2014-01-31 09:40:13

标签: c# asp.net design-patterns error-handling custom-error-handling

我在各种项目中遇到过这个问题几次,我想知道是否有比我通常最终使用的解决方案更好的解决方案。

假设我们有一系列需要执行的方法,我们想知道其中一个方法中是否出现问题并优雅地突破(可能撤消任何先前的更改......),我通常会这样做以下(伪C#,因为这是我最熟悉的):

private bool SomeMethod()
{
    bool success = true;
    string errorMessage = null;
    success = TestPartA(ref errorMessage);
    if (success)
    {
        success = TestPartB(ref errorMessage);
    }
    if (success)
    {
        success = TestPartC(ref errorMessage);
    }
    if (success)
    {
        success = TestPartD(ref errorMessage);
    }
        //... some further tests: display the error message somehow, then:
        return success;
}

private bool TestPartA(ref string errorMessage)
{
    // Do some testing...
    if (somethingBadHappens)
    {
       errorMessage = "The error that happens";
       return false;
    }
    return true;
}

我只是想知道(这是我的问题),如果有更好的方法来应对这种事情。我似乎最终写了很多if语句,看起来应该更流畅。

我被建议对一组委托函数进行循环,但我担心这会过度设计解决方案,除非有一个干净的方法来执行它。

3 个答案:

答案 0 :(得分:6)

我认为您应该使用例外。请注意,您通常只应在应用程序的“顶层”捕获异常。

private void TopLevelMethod()
{
    try
    {
        SomeMethod();
    }
    catch (Exception ex)
    {
        // Log/report exception/display to user etc.
    }
}

private void SomeMethod()
{
    TestPartA();
    TestPartB();
    TestPartC();
    TestPartD();
}

private void TestPartA()
{
    // Do some testing...
    try
    {
        if (somethingBadHappens)
        {
            throw new Exception("The error that happens");
        }
    }
    catch (Exception)
    {
        // Cleanup here. If no cleanup is possible, 
        // do not catch the exception here, i.e., 
        // try...catch would not be necessary in this method.

        // Re-throw the original exception.
        throw;
    }
}

private void TestPartB()
{
    // No need for try...catch because we can't do any cleanup for this method.
    if (somethingBadHappens)
    {
        throw new Exception("The error that happens");
    }
}

我在我的例子中使用了内置的System.Exception类;您可以创建自己的派生异常类,或使用从System.Exception派生的内置派生类。

答案 1 :(得分:4)

您可以尝试查看"Open/Closed" section of the SOLID Principle.在您的示例中,您可以创建一个ITestRule接口,其中包含一个名为CheckRule()的方法,该方法将更新您的消息并返回{{1} }}。然后,您将为要测试的每个规则创建一个接口实现,并将该类添加到bool对象。从上面的Redmondo示例中,我将更改为以下内容:

List<ITestRule>

然后,您将新的var discountRules = new List<ITestRule> { new TestPartA(), new TestPartB(), new TestPartC(), new TestPartD(), }; 传递给评估程序,该评估程序将循环遍历每个类并运行List<ITestRule>方法。

答案 2 :(得分:2)

我试图坚持一种称为'快速失败'的原则;方法应该在它们应该发生时失败,并立即返回错误的详细信息。然后调用方法适当地响应(将异常重新抛出给它的调用者,记录细节,如果它是一个UI绑定方法则显示错误等): -

http://en.wikipedia.org/wiki/Fail-fast

但是,这并不意味着使用异常来控制应用程序的流程。只是在 处理它时提出异常通常是不好的做法: -

http://msdn.microsoft.com/en-us/library/dd264997.aspx

在你的情况下,我会重新编写你的代码(例如): -

private bool SomeMethod()
{
    bool success = false;

    try
    {
        TestPartA();
        TestPartB();
        TestPartC();
        TestPartD();

        success = true;
    }
    catch (Exception ex)
    {
        LogError(ex.Message);
    }

    //... some further tests: display the error message somehow, then:
    return success;
}

private void TestPartA()
{
    // Do some testing...
    if (somethingBadHappens)
    {
        throw new ApplicationException("The error that happens");
    }
}