C# 忽略异常的最佳方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14389371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:41:34  来源:igfitidea点击:

C# Best way to ignore exception

c#exception

提问by John Ryann

Possible Duplicate:
Ignore Exception in 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>
                        }

采纳答案by JG in SD

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)
{ }

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.

如果确实忽略了这样的异常,最好在 catch 块中包含一些注释,说明为什么要忽略这样的异常。

回答by Lee

try
{
    DoBlah();
}
catch { }

回答by I4V

It could be something like this

它可能是这样的

try
{
    //blah
}
catch{}

If you want to ignore a specific exception

如果要忽略特定异常

try
{
    //blah
}catch(YourException){}