为什么空的catch块是坏主意? [英] Why are empty catch blocks a bad idea?

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

问题描述

我刚刚看到一个 try-catch的问题,哪些人(包括Jon Skeet)说空的catch块是一个非常糟糕的主意?为什么这个?有没有一个空的捕获不是错误的设计决策的情况?

I've just seen a question on try-catch, which people (including Jon Skeet) say empty catch blocks are a really bad idea? Why this? Is there no situation where an empty catch is not a wrong design decision?

我的意思是说,有时你想从某个地方获取一些额外的信息(webservice,database )你真的不在乎你是否会得到这个信息。所以你试图得到它,如果有什么事情发生的话,没关系,我只需要添加一个catch(Exception ignored){},这就是

I mean, for instance, sometimes you want to get some additional info from somewhere (webservice, database) and you really don't care if you'll get this info or not. So you try to get it, and if anything happens, that's ok, I'll just add a "catch (Exception ignored) {}" and that's all

推荐答案

通常空的try-catch是一个坏主意,因为你默默地吞咽一个错误条件,然后继续执行。偶尔这可能是正确的事情,但通常这是开发人员看到异常的一个迹象,不知道该怎么做,所以使用一个空的catch来保持这个问题。

Usually empty try-catch is a bad idea because you are silently swallowing an error condition and then continuing execution. Occasionally this may be the right thing to do, but often it's a sign that a developer saw an exception, didn't know what to do about it, and so used an empty catch to silence the problem.

相当于将黑色胶带放在引擎警示灯上的程序。

It's the programming equivalent of putting black tape over an engine warning light.

我相信你如何处理除了例外情况取决于您正在使用的软件层:雨林中的例外

I believe that how you deal with exceptions depends on what layer of the software you are working in: Exceptions in the Rainforest.

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

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