【问题标题】:Retry on exception in a try catch block [duplicate]重试try catch块中的异常[重复]
【发布时间】:2020-05-08 06:09:36
【问题描述】:

我正在尝试复制一个场景,如果捕获到异常,代码应该重新运行一定次数。

using System;
using System.Threading;

public class Program
{
    public static void Main()
    {
        MyClass myClass = new MyClass();
        Console.WriteLine(myClass.MyMethod(5));
    }
}

public class MyClass
{
    public Test(){}

    public string MyMethod(int tryCount) {          
        while(tryCount > 0)
        {
            // some logic that returns the switchType
            // in this case i'm just manually setting it to 1       
            int switchType = 1;

            try {
                switch(switchType)
                {
                    case 1:
                        return "it worked!!!";
                    case 2:
                        break;
                }
            } catch (Exception e){
                if (--tryCount == 0) throw new Exception("Failed");
            }
        }

        return null;
    }
}

如何强制它进入 catch 块,以便我可以测试我的重试逻辑是否正常工作?

【问题讨论】:

  • 不是真的,因为就我而言,为了测试,我想强制例外。
  • 它可能是在产生switchType 的代码中,而不是你想捕捉的,不是吗?
  • 没有办法,简单的switchints 会抛出异常......因此没有办法或理由来测试它。如果您有可能引发异常的东西(例如someClass.DoSomething())..那么您可以模拟该部分(例如someClassMock.Setup(mock => mock.DoSomething()).throws(new Exception())
  • @vc74 有道理,我会充实它以触发异常。除此之外是我的重试逻辑声音吗?

标签: c#


【解决方案1】:

对您的问题的最短回答是“抛出一个新异常”。

但您可能也希望能够测试 Happy Path。

这是我要做的:

using System;
using System.Threading;

public class Program
{
    public static void Main()
    {
        // what happens when no problem
        MyClass.happyPath = true;
        MyClass myClass = new MyClass();
        Console.WriteLine(myClass.MyMethod(5));

        // does the retry work?
        MyClass.happyPath = false;
        MyClass myClass = new MyClass();
        Console.WriteLine(myClass.MyMethod(5));
    }
}

public class MyClass
{

    public static boolean happyPath = true; 

    public Test(){}

    public string MyMethod(int tryCount) {          
        while(tryCount > 0)
        {
            // some logic that returns the switchType
            // in this case i'm just manually setting it to 1       
            int switchType = 1;

            try {
                switch(switchType)
                {
                    case 1:

                        if (!happyPath) {
                           throw new Exception ("Oops, it didn't work!");
                        }
                        return "it worked!!!";
            case 2:
                        break;
                }
            } catch (Exception e){
                if (--tryCount == 0) throw new Exception("Failed");
            }
        }

        return null;
    }

}

您在 break 语句中添加的 throw 应该被您的 catch 捕获,这允许您检查您的重试逻辑。建议您添加一些日志记录。

【讨论】:

  • 这正是我想要的。谢谢。另一方面,您觉得我的重试逻辑是否合理?
【解决方案2】:

您可以在测试时手动从代码中抛出异常。

throw new Exception("Your custom message");

【讨论】:

    猜你喜欢
    • 2014-07-24
    • 1970-01-01
    • 1970-01-01
    • 2017-10-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 2018-06-24
    相关资源
    最近更新 更多