是否存在适合使用空 catch 块的情况?

发布于 2024-10-12 04:58:57 字数 444 浏览 6 评论 0原文

可能的重复:
为什么空 catch 块不是一个好主意?
是否有任何正当理由忽略捕获的异常

你知道在什么情况下空 catch 块并不是绝对的邪恶吗?

try
{
    ...
    // What and When?
    ...
}
catch { }

Possible Duplicates:
Why are empty catch blocks a bad idea?
Is there any valid reason to ever ignore a caught exception

Do you know any situations when an empty catch block is not the absolute evil?

try
{
    ...
    // What and When?
    ...
}
catch { }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

月下凄凉 2024-10-19 04:58:57

这方面有很多问题,尝试看看:

为什么空的 catch 阻止了一个坏主意?

从该帖子接受的答案:

通常空的 try-catch 是一个坏主意,因为你会默默地吞下一个错误条件,然后继续执行。有时,这可能是正确的做法,但通常这表明开发人员看到了异常,不知道该怎么办,因此使用空 catch 来解决问题。

这在编程上相当于在引擎警告灯上贴上黑色胶带。

There are a lot of questions on this, try to look at:

Why are empty catch blocks a bad idea?

From that post's accepted answer:

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.

一百个冬季 2024-10-19 04:58:57

我想说,您至少应该提供某种评论或记录的消息,表明您在 try {} 中放入的内容引发了异常,这就是您不执行任何操作的原因。

I would say you should at least be providing some sort of comment or logged message indicating that what you put in the try {} threw an exception and this is why you aren't doing anything.

撞了怀 2024-10-19 04:58:57

看看这个,它基本上将您可能遇到的异常类型分为四类,其中任何一类都不应该由空的 catch 块处理。

Take a look at this, it basically breaks down the kind of exceptions you could encounter into four categories, none of which should be handled by an empty catch block.

谈情不如逗狗 2024-10-19 04:58:57

公理:

空的 catch 块绝对是邪恶的

不要试图找到解决这个问题的方法。仅仅试图找到它们不是绝对邪恶的案例就意味着你正在浪费宝贵的大脑周期。不要试图在这里找到模式,想着“嗯,我应该在这里放置一个空的 catch 块吗?”

如果您在某人的代码中偶然发现了一个空的 catch 块,那么您就偶然发现了技术债务。修复它。即使只是在空的 catch 块中添加一条日志记录语句,您也会让这个世界变得更美好。

Axiom:

Empty catch blocks are absolute evil

Don't try to find your way around this. Just by trying to find cases where they aren't absolute evil means you're wasting precious brain cycles. Don't try to find a pattern here, thinking "hmm, should I put an empty catch block here?"

If you stumble upon an empty catch block in somebody's code, you've just stumbled upon technical debt. Fix it. Even just by adding one logging statement inside an empty catch block, you'll make this world a better place.

你是我的挚爱i 2024-10-19 04:58:57

我将它用于一些自写的库,其中我需要某种 bool TrySomething(out object) 函数或 object TrySomething() ,其中底层调用不提供任何其他机制除外。在本例中,我使用空的 catch 块并返回 falsenull (取决于函数签名)。

证明空 catch 块的示例

public bool TrySomething(out object destination)
{
    try
    {
        destination = DoSomething();
        return true;
    }
    catch
    {}

    return false;
}

I used it for some self-written libraries where i need some kind of bool TrySomething(out object) function or object TrySomething() where the underlying call doesn't provide any other mechanism as an exception. In this case i use an empty catch block and return false or null (depending on the function signature).

Example to prove empty catch block

public bool TrySomething(out object destination)
{
    try
    {
        destination = DoSomething();
        return true;
    }
    catch
    {}

    return false;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文