I'm trying to replicate a scenario where the code should rerun a certain number of times if an exception is caught.
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;
}
}
How do I force it to go the catch block, so that I can test my re-try logic is working?