Ignore Exception in C#

I don’t think there is a trick to avoid exception but you can use the following code snippet:

public void IgnoreExceptions(Action act)
{
   try
   {
      act.Invoke();
   }
   catch { }
}

Using the method looks like:

IgnoreExceptions(() => foo());

Another solution is to use AOP (Aspect Oriented Programming) – there’s a tool called PostSharp which enables you to create an attribute that would catch all exceptions in specific assembly/class/method, which is closer to what you’re looking for.

Leave a Comment