“空语句"可能(不)抛出哪些 Java 错误和异常? [英] Which Java Errors and Exceptions may (not) be thrown by "empty statements"?

查看:39
本文介绍了“空语句"可能(不)抛出哪些 Java 错误和异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java.lang.Throwable 的哪个子类可能会被空语句抛出?

Which subclass(es) of java.lang.Throwable may be thrown by an empty statement?

空语句"一词是指无"、分号"和分号":

By the phrase "an empty statement", I'm referring to the "nothing", the "semi-colon", and the "semi-colons":

// ....
A(); B(); C();
try {
     // nothing
} catch (java.lang.Throwable e) {
     // which Throwable subclass might we see?
}
D(); E(); F();
try {
     ; // semi-colon
} catch (java.lang.Throwable e) {
     // which Throwable subclass might we see?
}
G(); H(); I();
try {
     ; ; ;;  ;;;;; ; ; ;;; ;; ;; ;; ;; ; ;; ; ;; // ... semi-colons
} catch (java.lang.Throwable e) {
     // which Throwable subclass might we see?
}
J(); K(); L();
// ....

Throwable 的哪些子类可以在 A();B(); 之间或 C(); 之间抛出D();F();G();I();J();?

Which subclasses of Throwable may be thrown between A(); and B(); or between C(); and D(); or between F(); and G(); or between I(); and J();?

或者更确切地说,哪些 Throwable 子类保证 出现在这些语句之间?

Or rather, which subclasses of Throwable are guaranteed not to appear between those statements?

我目前知道的InternalErrorOutOfMemoryErrorStackOverflowErrorUnknownError.

推荐答案

这个问题和你发布的另一个问题.我想我会在这里尝试解决这两个问题.

This question is very similar to the other question you posted. I think I'll try to address both questions here.

由于您指的是 JVMS,我假设您正在寻求正式的答案,而正式的答案是您的问题实际上没有意义.:-)

Since you refer to the JVMS I'll assume that you're after a formal answer, and the formal answer is that your question(s) doesn't really make sense. :-)

询问如何 JVM 将执行一段Java 源代码就像询问数学家正确 计算10+10 的方法.数学家可能会说如何计算它没有定义".同样,定义 Java 片段含义的 JLS 也没有详细说明 如何 执行它.

Asking how the JVM will execute a snippet of Java source code is like asking a mathematician the correct way of computing 10+10. The mathematician will probably say something like "how to compute it is not defined". Similarly, the JLS that defines the meaning of the Java snippet does not go into specifics of how to execute it.

所以,首先让我稍微形式化你的问题:在 bytecode 中(由 javac 的参考实现发出)对应于给定的 Java 片段可以 VirtualMachineErrors 发生了吗?"

So, first let me formalize your question slightly: "Where in the bytecode (emitted by the reference implementation of javac) corresponding to the given Java snippets could VirtualMachineErrors occur?"

这个问题可以说要简单得多.JVMS 的相关部分

This question is arguably much simpler to answer. The relevant section of the JVMS says

当内部错误或资源限制阻止它实现本章中描述的语义时,Java 虚拟机实现会抛出一个对象,该对象是类 VirtualMethodError 的子类的实例.本规范无法预测可能会遇到内部错误或资源限制的位置,也没有明确规定何时可以报告它们.

A Java Virtual Machine implementation throws an object that is an instance of a subclass of the class VirtualMethodError when an internal error or resource limitation prevents it from implementing the semantics described in this chapter. This specification cannot predict where internal errors or resource limitations may be encountered and does not mandate precisely when they can be reported.

因此,答案是:任意两条字节码指令之间.

Thus, the answer is: Between any two bytecode instructions.

现在回到你原来的问题:例如这个片段

Now to return to your original question: This snippet for instance

try {
     // nothing
} catch (java.lang.Throwable e) {
     // which Throwable subclass might we see?
}

编译成空程序,不能合理抛出任何异常.

is compiled to the empty program, which can't reasonably throw any exceptions.

关于您在评论中的后续问题:

Regarding your follow up question in a comment:

JLS 11.1.3 是否应该被解读为保证 Throwable 的子类不会出现在字节码之间,除非它是 VirtualMachineError 的子类"?

Should JLS 11.1.3 be read as "subclasses of Throwable are guaranteed not to appear between bytecode unless it is a subclass of VirtualMachineError"?

是的,你可以这样说.我可能会措辞有点不同:任何指令都可以引起

Yes, you could put it like that. I would perhaps have worded it a bit differently: Any instruction can give rise to

这篇关于“空语句"可能(不)抛出哪些 Java 错误和异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆