A stable modding interface between Techblox and mods https://mod.exmods.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
1022B

  1. using System;
  2. using GamecraftModdingAPI.Events;
  3. namespace GamecraftModdingAPI.Utility
  4. {
  5. public static class ExceptionUtil
  6. {
  7. /// <summary>
  8. /// Invokes an event in a try-catch block to avoid propagating exceptions.
  9. /// </summary>
  10. /// <param name="handler">The event to emit, can be null</param>
  11. /// <param name="sender">Event sender</param>
  12. /// <param name="args">Event arguments</param>
  13. /// <typeparam name="T">Type of the event arguments</typeparam>
  14. public static void InvokeEvent<T>(EventHandler<T> handler, object sender, T args)
  15. {
  16. try
  17. {
  18. handler?.Invoke(sender, args);
  19. }
  20. catch (Exception e)
  21. {
  22. EventRuntimeException wrappedException =
  23. new EventRuntimeException($"EventHandler with arg type {typeof(T).Name} threw an exception", e);
  24. Logging.LogWarning(wrappedException.ToString());
  25. }
  26. }
  27. }
  28. }