Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Catch method improvements #205

Closed
marcoerni opened this Issue · 9 comments

4 participants

@marcoerni

Hi

It would be nice, if the static Catch class could be extended to catch a specific type of exception like:

public static class Catch
{
    public static Exception Exception(Action throwingAction)
    {
        return Exception<Exception>(throwingAction);
    }

    public static TException Exception<TException>(Action throwingAction)
        where TException : Exception
    {
        try
        {
            throwingAction();
        }
        catch (TException ex)
        {
            return ex;
        }

        return null;
    }
}

The existing second overload with a Func delegate is not required and can be removed. You also can pass a function into the Action delegate.

Thanks
Marco

@ulrichb
Collaborator

@marcoerni: The Func<T>-overload is needed to test property-getters, see #168.

@danielmarbach

If we still need to support the Func overload we cannot support this. Because the compiler automatically chooses the overload without the generic constraint.

@danielmarbach

@ulrichb the overload Func was only added for property getter support. I think this is a very rare scenario. But as you originally sent the pullrequest I leave the decision up to you. What's your thoughts on this?

@marcoerni

What if when you name the methods like?

public static class Catch
{
public static Exception AllExceptions(Action throwingAction);
public static Exception AllExceptions(Func throwingAction);

public static TException OnlyException<TException>(Action throwingAction)
    where TException : Exception;

}

@danielmarbach
@ursenzler
Collaborator
@danielmarbach
@danielmarbach

@marcoerni Latest unstable 77 has this built in. Please provide feedback. By the way this package does no longer contain the should reference

@marcoerni

@danielmarbach Thanks for the fast feedback, I will test it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.