Using a general class for execution with try/catch/finally?
- by antirysm
I find myself having a lot of this in different methods in my code:
try
{
  runABunchOfMethods();
}
catch (Exception ex)
{
  logger.Log(ex);
}
What about creating this:
public static class Executor
{
    private static ILogger logger;
    public delegate void ExecuteThis();
    static Executor()
    {
        // logger = ...GetLoggerFromIoC();
    }
    public static void Execute(ExecuteThis executeThis)
    {
        try
        {
            executeThis();
        }
        catch (Exception ex)
        {
            logger.Log(ex);
        }
    }
}
And just using it like this:
private void RunSomething()
{
  Method1(someClassVar);
  Method2(someOtherClassVar);
}
...
Executor.Execute(RunSomething);
Are there any downsides to this approach? (You could add Executor-methods and delegates when you want a finally and use generics for the type of Exeception you want to catch...)