0

I have a program that calls dozens of methods with varying signatures, but the exception handling inside each one is identical. Is there some way to define a method that can accept a reference to a generic method with various signatures (which rules out a Delegate - right?) and return the object, or void that the method requires? I'm using .NET 4.72.

Here is stripped down version of what I'm currently doing and some pseudo-code of what I'd like to do:

 static class StackOverflowQuestion
{
    public static void Main(string[] args)
    {
        // What I'm currently doing:
        MethodOne("x");
        int ret = MethodTwo(0);
        //.
        //.
        //.
        MethodNineteen();

        // what I'd like to do is replace MethodOne(), MethodTwo(), ..., Method Nineteen()
        // with something like:
        RunMethod<void>(MethodOneWork, new object[] {"x"});
        ret = RunMethod<int>(MethodTwoWork, new object []{1});
        //.
        //.
        //.
        RunMethod<void>(MethodNineteenWork, null);          
    }

    private static void MethodOne(string st)
    {
        try
        {
            // the try clause is the only difference between the methods
            MethodOneWork(st);
        }
        catch (MyExceptionA)
        {
            HandleExceptionA();
            return;
        }
        catch(MyExceptionB)
        {
            HandleExceptionB();
        }
        catch(Exception)
        {
            HandleGenericException();
        }
    }

    private static int MethodTwo(int v)
    {
        try
        {
            return MethodTwoWork(v);
        }
        catch (MyExceptionA)
        {
            HandleExceptionA();
            return -1;
        }
        catch (MyExceptionB)
        {
            HandleExceptionB();
            return -2;
        }
        catch(Exception)
        {
            HandleGenericException();
            return 0;
        }          
    }

    private static void MethodNineteen()
    {
        try
        {
            MethodNineteenWork();
        }
        catch (MyExceptionA)
        {
            HandleExceptionA();
            return;
        }
        catch (MyExceptionB)
        {
            HandleExceptionB();
        }
        catch(Exception)
        {
            HandleGenericException();
        }
    }

    /// <summary>
    /// Run generic method with generic signature
    /// </summary>
    private static <T> RunMethod(Delegate MethodxWork, object[] myParams)
    {
        try
        {
            new <T>() retVal = MethodxWork(myParams);
            return retVal;
         }
        catch (MyExceptionA)
        {
            HandleExceptionA();
            return new <T>();
        }
        catch (MyExceptionB)
        {
            HandleExceptionB();
            return new <T>();
        }
        catch(Exception)
        {
            HandleGenericException();
            return new <T>();
        }
    }

    private static void HandleExceptionB()
    {
         //handle it
    }

    private static void HandleExceptionA()
    {
         //handle it
    }

    private static void HandleGenericException()
    {
        //handle it
    }


}

internal  class MyExceptionB : Exception
{
}

internal class MyExceptionA : Exception
{
}
3
  • Are you going to do something with return values of those methods? Commented Feb 28, 2020 at 23:12
  • Are you sure that exception handling in all methods will change for same reasons? Commented Feb 28, 2020 at 23:27
  • You said "exception handling inside each one is identical". If that's not the case please edit your question to clarify. Commented Feb 29, 2020 at 23:33

1 Answer 1

3

Sure, just create a few methods whose job it is to handle the exceptions, one for returning results and the other for void, and provide something that does your work.

T Handle<T>(Func<T> call)
{
    try
    {
        return call();
    }
    catch(YourException ex)
    {
        return default;
    }
}

void Handle(Action call)
{
    try
    {
        call();
    }
    catch(YourException ex)
    {

    }
}

After that, you can call your other methods with varying signatures inside there.

var result = Handle(() => SomeCallWithVaryingSignature(...));
Handle(() => SomeOtherCall(...));
Sign up to request clarification or add additional context in comments.

4 Comments

Issue can be when underlying methods have different return values for exception cases.
@Fabio - You said "exception handling inside each one is identical". If that's not the case please edit your question to clarify.
@Enigmativity, sorry that is not my question, but based on the code sample, exception handling is not identical because some methods have return value and some haven't. My concern is about scenario where return value of exception handling is different for some methods.
@Fabio - Oops. Sorry, my mistake.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.