Using a general class for execution with try/catch/finally?
Posted
by antirysm
on Stack Overflow
See other posts from Stack Overflow
or by antirysm
Published on 2010-06-11T08:16:34Z
Indexed on
2010/06/11
8:22 UTC
Read the original article
Hit count: 248
c#
|exception-handling
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...)
© Stack Overflow or respective owner