好的,这是有史以来最糟糕的编程示例之一,但我在调查其他人的问题时尝试了它,发现结果有点奇怪。有什么解释吗?
public class Test {
static class Bizarre extends RuntimeException {
public void throwMe() {
throw this; // line 6
}
}
public static void main(String[] args) {
Bizarre biz = new Bizarre(); // line 12
System.out.println("Output line 1"); // line 13
biz.throwMe(); // line 14
System.out.println("Output line 2"); // line 15
}
}
结果输出:
Output line 1
Exception in thread "main" Test$Bizarre
at Test.main(Test.java:12)
为什么是第 12 行?
最佳答案
Exception
(实际上是Throwable
)堆栈跟踪在初始化时创建。您的异常是一个 Bizarre
实例,是在第 12 行创建的。
关于java - "throw this"在回溯中产生奇怪的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24252034/