Ignore Exception in C#

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

    No. If an exception is thrown, it is usually a critical error which has happend. You do not want to ignore it.

    Instead you should rewrite your code to check for the errors and only if it really fails, a exception is cast.

    For example using Int32.TryParse instead of Int32.Parse to check if an object is an valid integer. Remember that exceptions are very expensive when they are cast and many casts severely affect performance of your application.

提交回复
热议问题