Consider this pair of Throwable
:
IllegalAccessException
extends 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.
IllegalAccessError
ext 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
andXXXException
combo? How are the pairs related to each other? - If you explicitly
try
tocatch
one in anException/Error
pair, should you alsocatch
the other?
The description already explains some of it: the
Exception
is thrown when you use reflection to access a field or invoke a method that is not accessible; theError
is thrown when you do so directly (and for some reason the compiler did not have the chance to catch it - for example when you have an old version of a class file around, in which the field or method you're trying to use is private).An
Error
normally indicates that there is something really wrong - there is almost certainly a bug in the software. You should never try to catchError
s. If you are catching theXXXException
, there is no immediate reason to also catch theXXXError
. The documentation ofError
says:An example to generate
IllegalAccessException
: Via reflection, lookup a private method in a class and try to call it.An example to generate
IllegalAccessError
: Create two classes and save them in two source filesA.java
andB.java
. In classA
, create a public method, and in classB
, call that method. Compile the source files. Now, editA.java
and make the method private, and recompile onlyA.java
(notB.java
). Now try running again;B
will try to call the method and throw anIllegalAccessError
.There are other pairs of
XXXException
/XXXError
that seem related, but they don't always have exactly matching names; for exampleClassNotFoundException
/NoClassDefFoundError
.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).There is a clear relationship between the two. The difference is the circumstances in which the two occur.
Pass. Check the javadocs.
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 separatingError
fromException
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.There are several pairs of Exception/Error in
java.lang
, and all of the following deal with reflective vs. direct usage:Other examples are:
Until now, I didn't know these two errors existed, and they are not referenced from other classes in the Javadoc (1.6). So I don't know if and when they are thrown.