TechbloxModdingAPI/GamecraftModdingAPI/Utility/ExceptionUtil.cs
NorbiPeti dba7c0a46f
Added 2 block events and removed BDNEException
Added an event for placing and removing blocks
Added a class to wrap event calls in a try-catch
Removed BlockDoesNotExistException
Made Block.GetSimBody() return null if block doesn't exist
2020-06-02 03:15:32 +02:00

29 lines
No EOL
1,022 B
C#

using System;
using GamecraftModdingAPI.Events;
namespace GamecraftModdingAPI.Utility
{
public static class ExceptionUtil
{
/// <summary>
/// Invokes an event in a try-catch block to avoid propagating exceptions.
/// </summary>
/// <param name="handler">The event to emit, can be null</param>
/// <param name="sender">Event sender</param>
/// <param name="args">Event arguments</param>
/// <typeparam name="T">Type of the event arguments</typeparam>
public static void InvokeEvent<T>(EventHandler<T> handler, object sender, T args)
{
try
{
handler?.Invoke(sender, args);
}
catch (Exception e)
{
EventRuntimeException wrappedException =
new EventRuntimeException($"EventHandler with arg type {typeof(T).Name} threw an exception", e);
Logging.LogWarning(wrappedException.ToString());
}
}
}
}