IllegalAccessError和IllegalAccessException之间的区别 [英] Difference between IllegalAccessError and IllegalAccessException

查看:1621
本文介绍了IllegalAccessError和IllegalAccessException之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这对 Throwable


IllegalAccessException extends Exception



当应用程序尝试反射创建一个实例(数组除外)时抛出,设置或获取字段或调用方法,但当前执行的方法无法访问指定的类,字段,方法或构造函数的定义。



IllegalAccessError ext IncompatibleClassChangeError ext LinkageError ext错误



如果应用程序尝试访问或修改字段或调用方法,它无法访问。



通常,该错误被编译器捕获;此错误只能在运行时发生,如果类的定义已经不相容地更改。




问题




  • 有人可以代码示例,其中每个都被抛出?

  • 名称中的相似性是否意味着两者之间的关系,还是纯粹的巧合?

  • 是否有其他 XXXError XXXException 组合? c code code code code code>一个异常/错误对,你还应该 catch 另一个?


解决方案


有人可以给出每个代码示例吗?


当您尝试使用反射来调用方法或读取或写入字段时,会抛出 IllegalAccessException 这是Java可见性规则所禁止的。



一致编译的Java代码不能抛出一个 IllegalAccessError 。例如,当您加载尝试调用方法的类时,或者在Java可见性规则禁止的另一个类中读取或写入一个字段。这是编译器通常会阻止的,所以这意味着有一些严重错误的类。无论如何,这被认为是一个错误;即不可恢复,类加载器将拒绝加载违规类。


名称中的相似性是否意味着两者之间的关系还是纯粹的巧合?


两者之间有明确的关系。不同的是两种情况发生。


还有其他XXXError和XXXException组合吗?双方如何相互关联?


通过。检查javadocs。


如果您明确地尝试在异常/错误对中捕获一个,是否还可以捕获另一个?


可能不是。 XXXError和XXXException通常发生在不同的情况。 (这肯定适用于反射和加载器的)。



此外,作为一般规则,您不应尝试从错误。从异常分离错误的全部要点是区分不可恢复和(可能)可恢复的异常。 p>

在这种情况下,正常应用程序无法从 IllegalAccessError 中恢复。如果您尝试重复导致问题的类加载器操作,那么它将再次发生。


Consider this pair of Throwable:

IllegalAccessExceptionextends Exception

Thrown when an application tries to reflectively create an instance (other than an array), set or get a field, or invoke a method, but the currently executing method does not have access to the definition of the specified class, field, method or constructor.

IllegalAccessErrorext IncompatibleClassChangeError ext LinkageError ext Error

Thrown if an application attempts to access or modify a field, or to call a method that it does not have access to.

Normally, this error is caught by the compiler; this error can only occur at run time if the definition of a class has incompatibly changed.

Questions

  • Can someone give a code example where each is thrown?
  • Does the similarity in name imply relationship between the two, or is it just pure coincidence?
  • Are there other XXXError and XXXException combo? How are the pairs related to each other?
  • If you explicitly try to catch one in an Exception/Error pair, should you also catch the other?

解决方案

Can someone give a code example where each is thrown?

The IllegalAccessException is thrown when you try to use reflection to invoke a method or read or write a field that is forbidden by the Java visibility rules.

An IllegalAccessError cannot be thrown by consistently compiled Java code. It occurs when for example, you load a class that attempts to invoke a method or read or write a field in another class that is forbidden by the Java visibility rules. This is something that the compiler would normally prevent, so this means there is something seriously wrong with the classes. At any rate, this is considered to be an "error"; i.e. not recoverable, and the classloader will refuse to load the offending class(es).

Does the similarity in name imply relationship between the two, or is it just pure coincidence?

There is a clear relationship between the two. The difference is the circumstances in which the two occur.

Are there other XXXError and XXXException combo? How are the pairs related to each other?

Pass. Check the javadocs.

If you explicitly try to catch one in an Exception/Error pair, should you also catch the other?

Probably not. The XXXError and XXXException generally occur in different circumstances. (This certainly applies to the reflective vs. classloader ones.)

Besides, as a general rule you should not attempt to catch and recover from subtypes of Error. The whole point of separating Error from Exception is to distinguish the non-recoverable and (potentially) recoverable exceptions.

In this case, there is nothing that a normal application can do in the way of recovering from the IllegalAccessError. If you attempt to repeat the classloader operation that caused the problem, it will just happen again.

这篇关于IllegalAccessError和IllegalAccessException之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆