在一个 catch 块中使用多个异常有什么好处?

发布于 2024-12-01 23:20:34 字数 683 浏览 0 评论 0原文

我们都听说过,在 Java 7 中我们可以这样写:

try {
   //something with files and IO
} catch (FileNotFoundException | IOException ex) {
    ex.printStackTrace();
    System.out.println("It's can't copy file");
}

而不是

try {
   //something with files and IO
} catch (FileNotFoundException wx) {
    ex.printStackTrace();
} catch (IOException ex) {
   ex.printStackTrace();
}

but,除了更短的代码之外,它还有什么真正的好处?
即使我们希望在每个 catch 块中执行相同的操作,我们也可以:

  1. 仅捕获 IOException,因为 FileNotFoundException 是一个子类型。
    或者
  2. 如果一个异常不是另一个异常的子类型,我们可以编写一些handleException()方法并在每个catch块中调用它。

那么,这个功能只是用于更干净的代码还是用于其他目的?

谢谢。

We've all heard that in Java 7 we can write:

try {
   //something with files and IO
} catch (FileNotFoundException | IOException ex) {
    ex.printStackTrace();
    System.out.println("It's can't copy file");
}

instead of

try {
   //something with files and IO
} catch (FileNotFoundException wx) {
    ex.printStackTrace();
} catch (IOException ex) {
   ex.printStackTrace();
}

but, what is it really good for besides shorter code?
Even if we want the same operations done in each catch block, we can:

  1. only catch IOException because FileNotFoundException is a subtype.
    or
  2. if one exception is not a subtype of the other one, we can write some handleException() method and call it in each catch block.

So, is this feature only used for cleaner code or for anything else?

Thanks.

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

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

发布评论

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

评论(5

冬天旳寂寞 2024-12-08 23:20:34

它不是为了让代码看起来更干净并节省击键次数。这些都是附加福利。

重复会导致不一致和错误。因此,通过创建一种不需要重复的语法,可以避免真正的错误。这就是 DRY 背后的动机——不是写得更快的漂亮代码。

It's not for making code look cleaner and saving key strokes. Those are fringe benefits.

Repetition leads to inconsistency and errors. So, by creating a syntax that makes repetition unnecessary, real errors can be avoided. That's the motivation behind DRY—not pretty code that is quicker to write.

無心 2024-12-08 23:20:34

是的,它主要是为了更干净的代码。

Yes, it is primarily for cleaner code.

它是为了更清晰的代码..但这非常有价值。在某些情况下,您可能会抛出 4 或 5 个异常,并且如果您在所有情况下想要做的都是相同的(通常是这种情况),那么就会减少大量代码。

这使得它更具可读性并且也更好可测试。这本身就足够有价值了。

It is for cleaner code .. but that is very valuable. In some cases you can have 4 or 5 exceptions thrown and if all you want to do is the same in all cases, which is often the case, that cuts down a lot of code.

This makes it more readable and also better testable. That in itself is valuable enough.

顾北清歌寒 2024-12-08 23:20:34

较短的代码意味着更少的维护代码 - 这是一个不小的优势。是的,我可以编写异常处理方法,但现在我不经常这样做,例如仅当异常在代码的不同位置抛出时。

The shorter code means less code for maintenance - that's not so small advantage. Yes, I can write exception-handling method, but now I do not do it so often, e.g. only when the exception is thrown in different places of code.

久夏青 2024-12-08 23:20:34

这个想法相当于以下之间的区别:

if (myVar == true && myOtherVar == false)
    // Logic

if (myVar == true)
{
    if (myOtherVar == false)
        // Same logic
}

只是更短、更清晰的代码。

That idea is equivalent to the difference between:

if (myVar == true && myOtherVar == false)
    // Logic

And

if (myVar == true)
{
    if (myOtherVar == false)
        // Same logic
}

It's just shorter, cleaner code.

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