java - "empty statements"可能会(不会)抛出哪些 Java 错误和异常?

标签 java operating-system jvm virtual-machine jls

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

通过短语“空语句”,我指的是“无”、“分号”和“分号”:

// ....
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();?

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


The ones I know so farInternalError , OutOfMemoryError , StackOverflowError , 和 UnknownError .

最佳答案

编译器可能会从字节码中删除包含“无”或空语句的代码。字节码中的等价物将完全类似于:

// ....
A(); B(); C();

D(); E(); F();

G(); H(); I();

J(); K(); L();
// ....

当然,在执行过程中,任何类型的意外错误(如UnknownError)都可能发生,通常不会在您的应用程序中处理它。

可能发生在任何地方的最接近的异常类型(强调是为了可能涵盖两个字节码指令之间的时间)是asynchronous exceptions :

Most exceptions occur synchronously as a result of an action by the thread in which they occur, and at a point in the program that is specified to possibly result in such an exception. An asynchronous exception is, by contrast, an exception that can potentially occur at any point in the execution of a program.

Asynchronous exceptions occur only as a result of:

  • An invocation of the (deprecated) stop method of class Thread or ThreadGroup.

    The (deprecated) stop methods may be invoked by one thread to affect another thread or all the threads in a specified thread group. They are asynchronous because they may occur at any point in the execution of the other thread or threads.

  • An internal error or resource limitation in the Java Virtual Machine that prevents it from implementing the semantics of the Java programming language. In this case, the asynchronous exception that is thrown is an instance of a subclass of VirtualMethodError.

但同样,没有必要关心这种类型的异常(VirtualMethodError 的子类),因为它们表示 JVM 执行中的严重错误。例如,这可能是由于用户使用 Ctrl+C 手动中断造成的。在这种情况下,您无能为力。

关于java - "empty statements"可能会(不会)抛出哪些 Java 错误和异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27679173/

相关文章:

linux - 接收信号时阻塞的系统调用会发生什么?

c - 在 C 中打印系统名称?

栈帧中的Java运行时常量池引用变量

java - JVM性能调优: young copy vs old generation gc

java - 文件夹目的地 - 上传文件

java - 从 GSON 解析 JSON 中的通用标签

java - 将字符串转换为颜色 int

windows - 使用 Visual C++ 2010 制作的 Windows 控制台应用程序的最低操作系统是什么?

java - java.exe、javaw.exe和jvm.dll的区别

在法国机器上执行时出现 java.lang.NumberFormatException