TryCatchExt.cs 980 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System;
  2. namespace Quadarax.Foundation.Core.Exceptions
  3. {
  4. public static class TryCatchExt
  5. {
  6. public static Exception TryCatch<TResult>(this object owner, Func<TResult> fnc)
  7. {
  8. if (owner == null)
  9. throw new ArgumentNullException(nameof(owner));
  10. if (fnc == null)
  11. return null;
  12. try
  13. {
  14. fnc();
  15. }
  16. catch (Exception e)
  17. {
  18. return e;
  19. }
  20. return null;
  21. }
  22. public static Exception TryCatch(this object owner, Action fnc)
  23. {
  24. if (owner == null)
  25. throw new ArgumentNullException(nameof(owner));
  26. if (fnc == null)
  27. return null;
  28. try
  29. {
  30. fnc();
  31. }
  32. catch (Exception e)
  33. {
  34. return e;
  35. }
  36. return null;
  37. }
  38. }
  39. }