空的catch块 [英] Empty catch blocks

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

问题描述

我有时碰到我需要捕捉异常,如果它丢进但从来没有做任何与它的情况。换句话说,可能出现异常,但如果这样做,也没有关系。

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

现在,你可能认为这样做是一个糟糕的主意,因为你将要返回给用户,并解释一个或多个消息无法发送给收件人。但是,如果它是什么只是一个抄送地址?这是不太重要的,你可能仍然要继续发送的消息,即使这些地址之一是无效的(可能只是一个错字)。

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块,如果你真的想要当某一类型的异常会发生什么都不做。你可以通过捕获只有类型,你的异常的改善您的例子期待的发生,并且你知道是安全的忽略。通过捕获例外,你可以隐藏的错误,并使其更难为自己调试程序。

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.

有关1号(外部的问题),这是毫无规则是什么正确的事情是做。这一切都取决于应用程序的细节。如果你想忽略某个条件,继续,那么这样做。

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答案...<咳嗽>)告诉你。但也有例外的每一个规则,人们写这些文章不知道你的应用程序的细节。你做。请确保你正在阅读的是有道理的,如果没有,相信你自己。

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.

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

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