public class HelloWorld {
static {
try {
int i=10/0;
} catch(ExceptionInInitializerError | ArithmeticException e ) {
e.printStackTrace();
}
}
public static void main(String []args) {
System.out.println("Hello World");
}
}
输出:
java.lang.ArithmeticException: / by zero
at HelloWorld.<clinit>(HelloWorld.java:7)
此代码没有实际用途。但只是想知道它为什么会ArithmeticException
摔倒ExceptionInInitializerError
。只是尝试了 multi-catch 语句并遇到了这个问题。
下面的代码抛出ExceptionInInitializerError
. 所以从逻辑上讲,如果我使用 try-multicatch,它应该 catch ExceptionInInitializerError
,但这里不是这样。有谁可以帮我离开这里吗。
public class HelloWorld {
static int i = 10/0;
public static void main(String []args){
System.out.println("Hello World");
}
}
输出:
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.ArithmeticException: / by zero
at HelloWorld.<clinit>(HelloWorld.java:4)