Functional C# - using or returning Action's

c#, higher-order-functions, metaprogramming

Solution

The second case is like a Faultable Action Factory. Where you pass in a delegate of what you want to do, `protectedBlock`, and a delegate of what to do when an `Exception` occurs, `faultHandler`. The actions are returned wrapped in a try/catch structure as an aggregate `Action`. My problem with both these methods is no `Exception` is actually being caught so who ever is going to catch your throw has no information on which to act.

The difference in execution between the 2 is when they actually execute. The 1st will be executed when it's called. The 2nd will execute whenever the returned `Action` is called. I don't think the efficiency difference would be significant.

Problem

Browsing the net for better fault handling in C#, I've com across the following to implementation strategies. The first one is natural to me, while the other implementation I'm not certain what its advantages are? 1) ` ``` static void Fault(Action protectedBlock, Action faultHandler) { try { protectedBlock(); } catch { faultHandler(); throw; } } ``` ` 2) ` ``` static Action Fault(Action protectedBlock, Action faultHandler) { return () => { try { protectedBlock(); } catch { faultHandler(); throw; } }; } ``` ` Is 2) the preferred strategy when developing higher order functions in C#? And, I am wondering, if one approach is more efficient than the other.

Original source