ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

AsDoesntThrow

Wraps an action with a try...catch of a specific exception

Source

public static Action AsDoesntThrow<T>(this Action action)
            where T : Exception
{
    return (() =>
        {
            try
            {
                action();
            }
            catch (T) {}
        });
}

Example

var thisActionThrows = () => { throw new NullReferenceException() };
var thisDoesntThrow = thisActionThrows.AsDoesntThrow<NullReferenceException>();

Author: ytoledano

Submitted on: 2 mrt. 2013

Language: C#

Type: Action

Views: 4894