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

查看:39
本文介绍了为什么空的 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?

我的意思是,例如,有时您想从某个地方(网络服务、数据库)获取一些附加信息,而您真的不在乎是否会获取这些信息.所以你试着去获取它,如果有任何事情发生,那没关系,我会添加一个catch (Exception ignore) {}",仅此而已

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 是一个坏主意,因为您正在默默地吞下错误条件,然后继续执行.有时这可能是正确的做法,但通常这表明开发人员看到了异常,不知道该怎么做,因此使用空捕获来解决问题.

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天全站免登陆