Using a message class static method taking in an action to wrap Try/Catch
Posted
by Chris Marisic
on Stack Overflow
See other posts from Stack Overflow
or by Chris Marisic
Published on 2010-04-07T21:57:41Z
Indexed on
2010/04/07
22:03 UTC
Read the original article
Hit count: 183
I have a Result object that lets me pass around a List of event messages and I can check whether an action was successful or not.
I've realized I've written this code in alot of places
Result result;
try
{
//Do Something
...
//New result is automatically a success for not having any errors in it
result = new Result();
}
catch (Exception exception)
{
//Extension method that returns a Result from the exception
result = exception.ToResult();
}
if(result.Success) ....
What I'm considering is replacing this usage with
public static Result CatchException(Action action)
{
try
{
action();
return new Result();
}
catch (Exception exception)
{
return exception.ToResult();
}
}
And then use it like
var result = Result.CatchException(() => _model.Save(something));
Does anyone feel there's anything wrong with this or that I'm trading reusability for obscurity?
© Stack Overflow or respective owner