0

I have a general retry on exception handler which I would like it to repeat a function for a definite number of time and here's the code for it

public static void Retry(this MethodInfo methodInfo, object[] parametrsList, short after = 0, short? retry = 1)
{
    if (retry < 0)
        return;
    try
    {
        short waitingPeriodMs = after*1000;
        Thread.Sleep(waitingPeriodMs);
        Type classObjType = methodInfo.ReflectedType;
        object classObj = Activator.CreateInstance(classObjType);
        methodInfo.Invoke(classObj, parametrsList);
    }
    catch (TargetInvocationException ex)
    {
        Debug.WriteLine("Exception Caught");
        methodInfo.Retry(parametrsList, after, --retry);
    }
    catch (Exception ex)
    {
        Debug.WriteLine("Exception Caught");
        methodInfo.Retry(parametrsList, after, --retry);
    }
}

The problem is whenever I an unhandled exception is throw in the method that I'm invoking neither of the two catch statements catches it I've made sure that the Use Only My Code checkbox is not checked

I've used those two Debug.Writeline statements to make sure that it's not a debugger related issue and checked the Output window and made sure those two statements were not executed

P.S. I'm aware the using a general retry on exception code is risky and could lead to an infinite number of retries the I'm using it because of a project related reason

Update: A unit test example that reproduces the question

[TestClass]
public class ExceptionTest
{
    [TestMethod]
    public void TestExceptionRetry()
    {
        Action act = () => { throw new Exception(); };
        act.Method.Retry(new object[0]);
    }
}
Scarnet
  • 738
  • 2
  • 11
  • 36
  • I don't really see how exception can bypass those catch blocks. Does it happen for all exceptions or for some specific ones? – Evk Apr 12 '17 at 14:44
  • is the method being invoked running in a separate thread? – KMoussa Apr 12 '17 at 14:48
  • @KMoussa yes it's being invoked in a separate thread – Scarnet Apr 12 '17 at 14:49
  • @Scarnet that's probably why the catch block isn't executed, see http://stackoverflow.com/questions/1554181/exception-handling-in-threads https://social.msdn.microsoft.com/Forums/vstudio/en-US/9957ec23-81a5-4ff9-9728-a863811e6ac4/catch-an-exception-from-one-thread-and-throw-to-main-thread?forum=csharpgeneral – KMoussa Apr 12 '17 at 14:53
  • @KMoussa that's not the case for me the retry method itself is being executed in a thread separate from the main thread but the method that it invokes never starts execution in a separate thread – Scarnet Apr 12 '17 at 14:57
  • and yes @Evk that is the case for all the exceptions – Scarnet Apr 12 '17 at 15:00
  • Would help to include code of the method being invoked (minimal example with which this problem is reproducable). – Evk Apr 12 '17 at 15:01
  • @Evk it's a code that calls a Web Method on another webserver. However, I tried it on a several methods that just throws different types of exceptions and it failed to catch any of them – Scarnet Apr 12 '17 at 15:03
  • Well I just copy-pasted your method and tested on a method which just do `throw new Exception()` which was happily caught. Maybe your method is async? – Evk Apr 12 '17 at 15:11
  • @Evk I tried it with a completely different project so I make sure it's not something related to configuration and replicated the same test scenario that you mentioned but still the Exception could not be caught, and no the method is not async – Scarnet Apr 13 '17 at 11:00
  • Well then the best you can do is compose minimal example application that reproduces the issue and include that in your question. – Evk Apr 13 '17 at 11:02
  • @Evk well apparently the Visual Studio debugger bug is still present, I tried logging catches to a database table and apparently it does access the catch boundaries it's just that the debugger doesn't detect that. However I have no idea what is the debugger option that is responsible for it if any – Scarnet Apr 13 '17 at 11:53
  • @Scamet - It's an option rather than a bug. To disable in Visual Studio -> Tools -> Options -> Debugging -> General and uncheck "Enable Just My Code" – Yogi May 24 '19 at 05:58

0 Answers0