简体   繁体   English

在运行时执行带有已检查异常的方法调用而不捕获它的行为

[英]Behavior of executing a method call with a checked exception at runtime without catching it

Suppose I have caller Class named let's say A in a jar file a.jar that calls a method let say Bb() in a separate jar file b.jar.假设我在 jar 文件 a.jar 中有一个名为 A 的调用者类,它调用一个方法让我们说 Bb() 在一个单独的 jar 文件 b.jar 中。

Until there everything is ok.直到那里一切正常。

Now B package developers are adding a checked Exception to Bb() which becomes something like Bb() throws Exception and redeploy the b.jar file.现在 B 包开发人员正在向 Bb() 添加一个已检查的异常,它变得类似于 Bb() 抛出异常并重新部署 b.jar 文件。 The a.jar file is kept intact, untouched so the A package developers do not notice the compile error requiring to catch the exception immediately. a.jar 文件保持完好无损,因此 A 包开发人员不会注意到需要立即捕获异常的编译错误。

What will happen at runtime ?运行时会发生什么? More precisely how the JVM deals with that.更准确地说,JVM 是如何处理的。 From my experience, but surprisingly it works smoothly in the case where no exceptions are actually thrown at runtime.根据我的经验,但令人惊讶的是,在运行时实际上没有抛出异常的情况下,它可以顺利运行。 So it looks that the method resolution is performed even if signature does not match at the exception level.所以看起来即使签名在异常级别不匹配,也会执行方法解析。 On the other hand, if Bb() actually throws an exception I have no idea of what will happen.另一方面,如果 Bb() 真的抛出异常,我不知道会发生什么。 Is this behaviour specified somewhere in the JVM or Java language specification or it is up to the JVM implementor or maybe even configurable with some JVM flags options ?这种行为是在 JVM 或 Java 语言规范中的某处指定的还是由 JVM 实现者决定的,或者甚至可以使用某些 JVM 标志选项进行配置?

Thanks for your clarifications感谢您的澄清

Exceptions are not part of a method's signature, at least, for identifying which method is intended.异常不是方法签名的一部分,至少,用于识别所使用的方法。 Furthermore, at the VM level, there is no such thing as a 'checked exception'.此外,在 VM 级别,没有“检查异常”这样的东西。 You can throw anything you like, even if the signature of your method doesn't declare it.你可以抛出任何你喜欢的东西,即使你的方法的签名没有声明它。 HOw do you think lombok's SneakyThrows, or scala works?你认为 lombok 的 SneakyThrows 或 scala 的工作原理如何?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM