Advertisement
Guest User

Untitled

a guest
May 19th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. public static class Tryable
  2. {
  3. public static Exceptional<T> Try<T>(Func<T> func)
  4. {
  5. try
  6. {
  7. return Exceptional.Ok(func());
  8. }
  9. catch (Exception e)
  10. {
  11. return Exceptional.Fail<T>(e);
  12. }
  13. }
  14. public static async Task<Exceptional<T>> Try<T>(Task<Func<T>> funcTask)
  15. {
  16. Func<T> func = await funcTask;
  17. T t = func();
  18. try
  19. {
  20. return Exceptional.Ok(t);
  21. }
  22. catch (Exception e)
  23. {
  24. return Exceptional.Fail<T>(e);
  25. }
  26. }
  27.  
  28. public static async Task<Exceptional<T>> Try<T>(Func<Task<T>> func)
  29. {
  30. try
  31. {
  32. T t = await func();
  33. return Exceptional.Ok(t);
  34. }
  35. catch (Exception e)
  36. {
  37. return Exceptional.Fail<T>(e);
  38. }
  39. }
  40.  
  41. public static async Task<Exceptional<ValueTuple>> Try(Func<Task> func)
  42. {
  43. try
  44. {
  45. await func();
  46. return Exceptional.Ok(new ValueTuple());
  47. }
  48. catch (Exception e)
  49. {
  50. return Exceptional.Fail<ValueTuple>(e);
  51. }
  52. }
  53.  
  54.  
  55. public static Exceptional<ValueTuple> Try(Action action)
  56. {
  57. return Try(action.ToFunc());
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement