Functional C # - using or returning Action

Browsing the network for better error handling in C #, I will talk about the following implementation strategies. The first is natural for me, but in another implementation I'm not sure 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 wonder if one approach is more effective than another.

+3
source share
3 answers

Faultable Action Factory. , , protectedBlock , , Exception, faultHandler. , try/catch Action. , Exception , - , , .

2 , . , . , Action. , .

+5

(2) , (1) . "", Action ( Func<A, R>).

, (2) :

Fault(someAction, Fault(firstTryFaultHandler, lastDitchFaultHandler))();

... . (1)

+4

# 2 . "Fault (a, b)"; , , , b . , . , .

, 2 , "Fault (a, b)();", . .

1. , "() = > Fault (a, b)".

+1

Source: https://habr.com/ru/post/1771733/


All Articles