-
JAVA doc中描述Class.forName(String className)方法在调用时,除了最常出现的ClassNotFoundException,还有两个错误:
@exception LinkageError if the linkage fails @exception ExceptionInInitializerError if the initialization provoked by this method fails -
看到LinkageError这个错误非常陌生,似乎没见过,查看doc,这个错误描述的是在编译后所引用的类发生了改变。
Subclasses of LinkageError indicate that a class has some dependency on another class; however, the latter class has incompatibly changed after the compilation of the former class.这应该是JVM在类加载过程之中解析阶段所抛出的错误
-
查看它的部分子类:
LinkageError ----IncompatibleClassChangeError --------AbstractMethodError --------IllegalAccessError --------InstantiationError --------NoSuchFieldError --------NoSuchMethodError这些子类看起来都比较眼熟,好像在jar包冲突的时候有见过类似的,翻看一下,它们都有对应的Exception类,那么这两者之间有什么区别和联系呢?
在网上没有搜索到有关答案,就暂且认为反射在加载类时抛出Error;在通过反射访问类相关信息时抛出的是Exception,鼓励用户处理这些异常。 -
所以,把LinkageError视作类加载的解析阶段出现异常






网友评论