C#最好的方式来忽略异常 [英] C# Best way to ignore exception

查看:662
本文介绍了C#最好的方式来忽略异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

在C#中忽略异常

有时在极少数情况下,你真的想要忽略异常。什么是最好的方法呢?我的方法是抓住异常,但不要做任何事情。其他?

Sometimes in rare circumstances you really want to just ignore exception. What is the best way to do this? my approach is catch the exception but dont do anything about it. others?

                        try
                        {
                            blah
                        }
                        catch (Exception e)
                        {
                            <nothing here>
                        }


推荐答案

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

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.

这篇关于C#最好的方式来忽略异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆