第二次捕获断裂汇编(第一个不及

发布于 2025-02-05 01:22:29 字数 517 浏览 2 评论 0原文

我有此代码:

try {
//  if (false) { //(need to add this, otherwise it doesn't compile)
//      throw new CountDownExc(-1);
//  }

    return ad2;
} catch (StackOverflowExc | NoClassDefFoundError e) {
    throw new CountDownExc(50);
} catch (CountDownExc e) {
    if (!e.surfaced()) {
        e.dec();
        throw e;
    }
    return 0.0;
}

如果我不添加注释的代码,则不会与:

错误:异常CountdownExc永远不会抛出相应的主体 尝试语句

为什么?

最重要的是,为什么第一个捕获不会导致编译器抱怨(但第二个障碍似乎是错误的)

I have this code:

try {
//  if (false) { //(need to add this, otherwise it doesn't compile)
//      throw new CountDownExc(-1);
//  }

    return ad2;
} catch (StackOverflowExc | NoClassDefFoundError e) {
    throw new CountDownExc(50);
} catch (CountDownExc e) {
    if (!e.surfaced()) {
        e.dec();
        throw e;
    }
    return 0.0;
}

And if I don't add the commented code it doesn't compile with a:

error: exception CountDownExc is never thrown in body of corresponding
try statement

Why?

Most of all, why does the first catch doesn't cause the compiler to complain (but the second block seems to be wrong)

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

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

发布评论

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

评论(1

虐人心 2025-02-12 01:22:29

您的代码存在两个问题:

  1. 首先,您需要了解检查的异常和未选中的例外之间的区别:理解Java中的检查已检查的与未检查的例外

本质上,您需要将检查的异常视为逻辑的一部分,您只能将其重新启动或通过catch> catch来处理它们。如果您需要避免catch(CountDownExc)是将抛出CountDownExc声明声明到您的方法中,则唯一的其他选项是。

  1. 这些行对我来说毫无意义:
} catch (StackOverflowExc | NoClassDefFoundError e) {
    throw new CountDownExc(50);

看起来您打算在第一个catch块中捕获两种异常类型,在第一个捕获块中投掷CountdownExc,然后在第二个catch块。这不会飞,因为catch阻止try块中抛出的东西,而不是catch block中。由于CountDownExc是一个检查的例外,这将导致另一个汇编错误,除非您要么将try - catch 在另一个中包装 catch 尝试 - catch(不良练习)或添加投掷声明。

由于您的catch throw CountdownExc的异常必须在其他地方处理,因此可能已经考虑到 - 我无法分辨,因为您的其余代码未知。

然而,您的直接问题来自以下事实,您的尝试块中没有任何东西可以抛出CountDownExcCountDownExc是一个检查的例外,请参见第一关联。

There is a couple of problems with your code:

  1. First and foremost you need to understand the difference between checked exceptions and unchecked exceptions: Understanding checked vs unchecked exceptions in Java

Essentially you need to consider checked exceptions as part of your logic and you can only either rethrow them or handle them via catch. The only other option if you need to avoid catch( CountDownExc ) is to add a throws CountDownExc declaration to your method.

  1. These lines make little sense to me:
} catch (StackOverflowExc | NoClassDefFoundError e) {
    throw new CountDownExc(50);

It looks like you intend on catching two exception types in the first catch block, throw CountDownExc in that first catch block, then handle it in the second catch block. That's not gonna fly, because catch blocks handle what is thrown in a try block, not in a catch block. Since CountDownExc is a checked exception this is going to result in another compilation error, unless you either wrap your try-catch in another try-catch (bad practice) or add a throws declaration.

Since both of your catches throw CountDownExc the exception will have to be handled elsewhere anyway, so this may already be accounted for - I cannot tell, because the rest of your code is unknown.

Your immediate problem however comes from the fact, that nothing in your try block is able to throw CountDownExc and CountDownExc is a checked exception, see first link.

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