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");
}
}
출력 :시도-multicatch있는 ExceptionInInitializerError와 arithmeticexception이 혼란
java.lang.ArithmeticException:/by zero
at HelloWorld.<clinit>(HelloWorld.java:7)
이 코드에 실제 목적이 없습니다. 그러나 ArithmeticException
을 ExceptionInInitializerError
에 던진 이유가 궁금했습니다. 그냥 멀티 캐치 선언문을 시험해보고 이것에 부딪쳤다.
아래 코드는 ExceptionInInitializerError
입니다. 따라서 논리적으로 try-multicatch를 사용하면 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)
실제 예외 ('ArithmeticException')가 먼저 발생합니다. catch하지 않으면 정적 초기화 완료시'ExceptionInInitializerError'로 래핑됩니다. 그러나 그것을 잡으면 즉시 정적 초기화 코드 _에 스택 추적을 인쇄합니다. 'ExceptionInInitializerError'를 얻었는지 더 자세히 살펴 보았습니까? –