且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#***的方式来忽略异常

更新时间:2022-12-31 08:45:00

如果你要捕获,不处理异常,忽略它,你可以简化你的一些。

If you are going to just catch, not handle the exception, and ignore it, you can simplify what you have slightly.

try
{
   // code
}
catch
{ }

以上是任何例外,如果你只想忽略某个例外但让别人泡泡,你可以这样做

The above is for any exception, if you only want to ignore a certain exception but let others bubble out, you can do this

try
{
   // code
}
catch (SpecificException)
{ }

如果你忽略这样的异常,***在catch块中添加一些注释,说明你为什么忽略这样的异常。

If you do ignore exceptions like this, it is best to include some comment in the catch block as to why you are ignoring the exception like that.