Ignore Exception in C#

后端 未结 9 739
梦毁少年i
梦毁少年i 2020-11-29 11:15

Is there a better way to ignore an exception in C# than putting it up in a try catch block and doing nothing in catch? I find this syntax to be cumbersome. For a codeblock,

9条回答
  •  粉色の甜心
    2020-11-29 11:31

        public static void Ignore(Action a) where T : Exception
        {
            try
            {
                a();
            }
            catch (T)
            {
            }
        }
    

    To use:

        Ignore(() => foo());
    

提交回复
热议问题