Ignore Exception in C#

后端 未结 9 755
梦毁少年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:49

    You can do it with AOP. Postsharp for example will allow you to easily implement such an attribute which will skip particular exceptions in methods to which you applied such an attribute. Without AOP I do not see any good way to do that (if we assume that there is a good way to do such things ;) ).

    With Postsharp you will be able to decorate your methods in this way:

    [IgnoreExceptions(typeof(NullReferenceException), typeof(StackOverflowException))]
    void MyMethod() { ... }
    

提交回复
热议问题