debugcsharpMinor
Encapsulating common Try-Catch code. Is this a known pattern? Is it good or bad?
Viewed 0 times
thiscatchbadencapsulatingknowngoodcodecommonpatterntry
Problem
In an effort to reduce code duplication, I often use and have used this style to capture handling of exceptions on a boundary of an application:
Given the following extension methods:
In the code I will use these (e.g. in a Windows/WCF/Web Service) to either silently catch the error and let the code continue to run (used in a polling service) or to at least log the error locally on the server and then rethrow the full exception again.
I have used several other variations where the exception gets wrapped in more 'user friendly' exceptions or in WCF
So typically this code is used as follows:
```
Response ISomeService.SomeOperation(Request request)
{
return _logger.TryCatchLogThrow(() => _domainImplementation.SomeOperation(request));
}
OtherResponse ISomeService.OtherOperation(OtherRequest request)
{
return _logger.TryCatchLogThrow(() => _domainImplementati
Given the following extension methods:
public static class FuncExtenstion
{
[DebuggerStepThrough]
public static Action AsAction(this Func f)
{
return (a) => f(a);
}
[DebuggerStepThrough]
public static Func AsFunc(this Action a)
{
return () => { a(); return true; };
}
}
public static class IlogExtensions
{
public static TResult TryCatchLogThrow(this ILog logger, Func f)
{
try
{
return f();
}
catch (Exception ex)
{
logger.Error(ex.Message, ex);
throw;
}
}
public static void TryCatchLogThrow(this ILog logger, Action f)
{
logger.TryCatchLogThrow(f.AsFunc());
}
public static TResult TryCatchLog(this ILog logger, Func f)
{
try
{
return f();
}
catch (Exception ex)
{
logger.Error(ex.Message, ex);
return default(TResult);
}
}
public static void TryCatchLog(this ILog logger, Action f)
{
logger.TryCatchLog(f.AsFunc());
}
}In the code I will use these (e.g. in a Windows/WCF/Web Service) to either silently catch the error and let the code continue to run (used in a polling service) or to at least log the error locally on the server and then rethrow the full exception again.
I have used several other variations where the exception gets wrapped in more 'user friendly' exceptions or in WCF
FaultExceptionsSo typically this code is used as follows:
```
Response ISomeService.SomeOperation(Request request)
{
return _logger.TryCatchLogThrow(() => _domainImplementation.SomeOperation(request));
}
OtherResponse ISomeService.OtherOperation(OtherRequest request)
{
return _logger.TryCatchLogThrow(() => _domainImplementati
Solution
This looks like the Execute Around pattern in its static syntax. A thunk is passed around, and then invoked at the appropriate moment.
The intent is however a bit different. In true execute around, the is something done before and something done after a certain action/function/block. Catching exceptions "feels" a bit different.
The intent is however a bit different. In true execute around, the is something done before and something done after a certain action/function/block. Catching exceptions "feels" a bit different.
Context
StackExchange Code Review Q#11999, answer score: 2
Revisions (0)
No revisions yet.