Skip to content

Instantly share code, notes, and snippets.

@liortal53
Created October 21, 2023 18:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liortal53/c3ef0110da0310c1f16394456dceab67 to your computer and use it in GitHub Desktop.
Save liortal53/c3ef0110da0310c1f16394456dceab67 to your computer and use it in GitHub Desktop.
Example demonstrating how to safely invoke all delegates in a System.Action while catching exceptions
using System;
namespace Extensions.Utils
{
public static class ActionExtensions
{
/// <summary>
/// Safe invocation of all delegates stored in the passed <see cref="Action"/> delegate.
/// </summary>
public static void InvokeSafe(this Action action)
{
if (action == null)
{
return;
}
foreach (Action a in action.GetInvocationList())
{
try
{
a.Invoke();
}
catch (Exception e)
{
// TODO: Debug.LogException ?
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment