ExceptionInInitializerError

ExceptionInInitializerError

本文介绍了是否可以捕获ExceptionInInitializerError?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何Throwable都可以被捕获

Any Throwable can be caught

class CatchThrowable {
  public static void main(String[] args){
    try{
      throw new Throwable();
    } catch (Throwable t){
      System.out.println("throwable caught!");
    }
  }
}

输出:

throwable caught!

因此,如果我在初始化块期间做了一些不好的事情,我希望能够捕获ExceptionInInitializerError。但是,以下方法不起作用:

So, if I do something bad during an initialization block, I'd expect to be able to catch an ExceptionInInitializerError. However, the following doesn't work:

class InitError {
  static int[] x = new int[4];
  static {                                                           //static init block
    try{
      x[4] = 5;                                                      //bad array index!
    } catch (ExceptionInInitializerError e) {
      System.out.println("ExceptionInInitializerError caught!");
    }
  }
  public static void main(String[] args){}
}

输出:

java.lang.ExceptionInInitializerError
Caused by: java.lang.ArrayIndexOutOfBoundsException: 4
    at InitError.<clinit>(InitError.java:13)
Exception in thread "main"

如果我将代码更改为另外捕获一个ArrayIndexOutOfBoundsException

and if I change the code to additionally catch an ArrayIndexOutOfBoundsException

class InitError {
  static int[] x = new int[4];
  static {                                                           //static init block
    try{
      x[4] = 5;                                                      //bad array index!
    } catch (ExceptionInInitializerError e) {
      System.out.println("ExceptionInInitializerError caught!");
    } catch (ArrayIndexOutOfBoundsException e){
      System.out.println("ArrayIndexOutOfBoundsException caught!");
    }
  }
  public static void main(String[] args){}
}

它被捕获的ArrayIndexOutOfBoundsException:

it's the ArrayIndexOutOfBoundsException that gets caught:

ArrayIndexOutOfBoundsException caught!

有人可以告诉我为什么会这样吗?

Could anyone tell me why that is?

推荐答案

顾名思义, ExceptionInInitializerError 是一个错误,不是例外。与例外情况不同,。它们表示致命的不可恢复状态,并且意味着停止你的程序。

As its name implies, ExceptionInInitializerError is an error, not an exception. Unlike exceptions, errors are not meant to be caught. They indicate fatal unrecoverable states, and are meant to stop your program.

ExceptionInInitializerError 表示一个 static 变量引发了一个未被捕获的异常 - 在你的情况下,它是 ArrayIndexOutOfBoundsException ,但是任何异常都会导致这个错误。由于静态初始化发生在正在运行的程序的上下文之外,因此无法传递异常。这就是为什么Java会产生错误而不是传递异常的原因。

ExceptionInInitializerError indicates that an initializer of a static variable threw an exception that has not been caught - in your case, it's ArrayIndexOutOfBoundsException, but any exception will cause this error. Since static initialization happens outside the context of your running program, there is no place to deliver the exception. That is why Java produces an error rather than delivering an exception.

这篇关于是否可以捕获ExceptionInInitializerError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 00:11