空捕获块 [英] Empty catch blocks

查看:14
本文介绍了空捕获块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有时会遇到这样的情况,如果它曾经抛出过,我需要捕获异常,但从不对其进行任何处理.换句话说,异常可能发生,但是否发生无关紧要.

I sometimes run into situations where I need to catch an exception if it's ever thrown but never do anything with it. In other words, an exception could occur but it doesn't matter if it does.

我最近阅读了这篇关于类似事情的文章:http://c2.com/cgi/wiki?EmptyCatchClause

I recently read this article about a similar thing: http://c2.com/cgi/wiki?EmptyCatchClause

此人如何评论

// should never occur 

是一种代码异味,不应出现在代码中.然后他们继续解释评论

is a code smell and should never appear in code. They then go onto explain how the comment

// don't care if it happens

完全不同,我自己也遇到过这样的情况.例如,在发送电子邮件时,我会执行类似的操作:

is entirely different and I run into situations like this myself. For example, when sending email I do something similar to this:

var addressCollection = new MailAddressCollection();
foreach (string address in addresses)
{
    try
    {
        addressCollection.Add(address);
    }
    catch (Exception)
    {
        // Do nothing - if an invalid email occurs continue and try to add the rest
    }
}

现在,您可能认为这样做是个坏主意,因为您想返回给用户并解释无法将一条或多条消息发送给收件人.但如果它只是一个 CC 地址呢?这不太重要,即使其中一个地址无效(可能只是拼写错误),您仍然可能仍想发送消息.

Now, you may think that doing this is a bad idea since you would want to return to the user and explain that one or more messages could not be sent to the recipient. But what if it's just a CC address? That's less important and you may still want to send the message anyway even if one of those addresses was invalid (possibly just a typo).

那么我使用空的 catch 块是否正确,还是有我不知道的更好的替代方法?

So am I right to use an empty catch block or is there a better alternative that I'm not aware of?

推荐答案

如果您真的想在发生某种类型的异常时什么都不做,那么使用空的 catch 块是完全正确的.您可以通过仅捕获您期望发生的异常类型来改进您的示例,并且您知道可以安全地忽略这些异常类型.通过捕获Exception,您可以隐藏错误并使自己更难调试程序.

You are completely right to use an empty catch block if you really want to do nothing when a certain type of exception occurs. You could improve your example by catching only the types of exceptions which you expect to occur, and which you know are safe to ignore. By catching Exception, you could hide bugs and make it harder for yourself to debug your program.

关于异常处理要记住的一件事:异常之间有很大的不同,异常用于表示程序外部的错误情况,至少有时预计会发生,并且指示编程错误的异常.第一个示例是异常,表明由于连接超时而无法传送电子邮件,或者由于没有磁盘空间而无法保存文件.第二种情况的一个例子是异常,表明您试图将错误类型的参数传递给方法,或者您试图越界访问数组元素.

One thing to bear in mind regarding exception handling: there is a big difference between exceptions which are used to signal an error condition external to your program, which is expected to happen at least sometimes, and exceptions which indicate a programming error. An example of the 1st would be an exception indicating that an e-mail couldn't be delivered because the connection timed out, or a file couldn't be saved because there was no disk space. An example of the 2nd would be an exception indicating that you tried to pass the wrong type of argument to a method, or that you tried to access an array element out of bounds.

对于第二个(编程错误),仅仅吞下"异常将是一个很大的错误.最好的做法通常是记录堆栈跟踪,然后弹出一条错误消息,告诉用户发生了内部错误,他们应该将日志发送回开发人员(即您).或者在开发过程中,您可能只是让它向控制台打印堆栈跟踪并导致程序崩溃.

For the 2nd (programming error), it would be a big mistake to just "swallow" the exception. The best thing to do is usually to log a stack trace, and then pop up an error message telling the user that an internal error has happened, and that they should send their logs back to the developers (i.e. you). Or while developing, you might just make it print a stack trace to the console and crash the program.

对于第一个(外部问题),没有关于正确"的事情是做什么的规则.这一切都取决于应用程序的细节.如果您想忽略某个条件并继续,那么就这样做.

For the 1st (external problem), there is no rule about what the "right" thing is to do. It all depends on the details of the application. If you want to ignore a certain condition and continue, then do so.

一般情况:

阅读技术书籍和文章很好.你可以从中学到很多东西.但是请记住,在您阅读时,您会发现很多人的建议,他们说做这样那样的事情总是 错误或总是 正确.通常,这些观点与宗教有关.永远相信以某种方式做事绝对是正确的",因为一本书或文章(或关于 SO... <cough> 的答案)告诉了你.每条规则都有例外,撰写这些文章的人不知道您的应用程序的详细信息.你做.确保您正在阅读的内容有意义,如果没有,请相信自己.

It's good that you are reading technical books and articles. You can learn a lot from doing so. But please remember, as you read, you will find lots of advice from people saying that doing such-and-such a thing is always wrong or always right. Often these opinions border on religion. NEVER believe that doing things a certain way is absolutely "right" because a book or article (or an answer on SO... <cough>) told you so. There are exceptions to every rule, and the people writing those articles don't know the details of your application. You do. Make sure that what you are reading makes sense, and if it doesn't, trust yourself.

这篇关于空捕获块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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