gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 15:09:11 24 4
gpt4 key购买 nike

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com