Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class Tryable
- {
- public static Exceptional<T> Try<T>(Func<T> func)
- {
- try
- {
- return Exceptional.Ok(func());
- }
- catch (Exception e)
- {
- return Exceptional.Fail<T>(e);
- }
- }
- public static async Task<Exceptional<T>> Try<T>(Task<Func<T>> funcTask)
- {
- Func<T> func = await funcTask;
- T t = func();
- try
- {
- return Exceptional.Ok(t);
- }
- catch (Exception e)
- {
- return Exceptional.Fail<T>(e);
- }
- }
- public static async Task<Exceptional<T>> Try<T>(Func<Task<T>> func)
- {
- try
- {
- T t = await func();
- return Exceptional.Ok(t);
- }
- catch (Exception e)
- {
- return Exceptional.Fail<T>(e);
- }
- }
- public static async Task<Exceptional<ValueTuple>> Try(Func<Task> func)
- {
- try
- {
- await func();
- return Exceptional.Ok(new ValueTuple());
- }
- catch (Exception e)
- {
- return Exceptional.Fail<ValueTuple>(e);
- }
- }
- public static Exceptional<ValueTuple> Try(Action action)
- {
- return Try(action.ToFunc());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement