Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java - IllegalAccessException



The Java IllegalAccessException is thrown when an application tries to reflectively create an instance (abstract class, interface, array class, primitive type, or void) that is not accessible. The IllegalAccessException is a checked exception that is thrown when you try to access a field, method, or constructor that is not accessible.

Following is the reason when JVM throws an IllegalAccessException in Java:

  • When the user tries to access a field, method, or constructor that is not accessible, JVM throws an IllegalAccessException.

Constructors of IllegalAccessException

There are two constructors of IllegalAccessException class:

  • IllegalAccessException(): This constructor is used to create an IllegalAccessException object without any message.
  • IllegalAccessException(String message): This constructor is used to create an IllegalAccessException object with a message.

Methods of IllegalAccessException

There are some methods of IllegalAccessException class:

Method Description
getMessage() It is used to return the message of the exception.
toString() It is used to return the detail message string of the exception.
printStackTrace() It is used to print the stack trace of the exception.

Example of IllegalAccessException

In this example, we are trying to access a field that is not accessible, so JVM will throw an IllegalAccessException.

import java.lang.reflect.Field;

public class IllegalAccessExceptionExample {
   public static void main(String[] args) {
      try {
         Class<?> cls = Class.forName("java.lang.String");
         Field field = cls.getDeclaredField("value");

         // Try to access a private field without setting it accessible
         System.out.println(field.get("Hello")); // This will throw IllegalAccessException

      } catch (ClassNotFoundException e) {
         e.printStackTrace();
      } catch (NoSuchFieldException e) {
         e.printStackTrace();
      }
   }
}

Output

Following is the output of the above code:

IllegalAccessExceptionExample.java:9: error: unreported exception IllegalAccessException; must be caught or declared to be thrown
System.out.println(field.get("Hello"));

Handling IllegalAccessException

In this example, we are trying to access a field that is not accessible, so JVM will throw an IllegalAccessException. We are handling this exception using try-catch block.

import java.lang.reflect.Field;
public class IllegalAccessExceptionExample {

   public static void main(String[] args) {
      try {
         Class<?> cls = Class.forName("java.lang.String");
         Field field = cls.getDeclaredField("value");
         field.setAccessible(false);
         System.out.println(field.get("Hello"));
      } catch(IllegalAccessException e) {
         System.out.println("Can't access a field that is not accessible");
      } catch(ClassNotFoundException e) {
         e.printStackTrace();
      } catch(NoSuchFieldException e) {
         e.printStackTrace();
      }
   }
}

Output

Following is the output of the above code:

Can't access a field that is not accessible

How to avoid IllegalAccessException?

There are some ways to avoid IllegalAccessException in Java:

  • Always check the accessibility of the field, method, or constructor before accessing it.
  • Always use the setAccessible() method to set the accessibility of the field, method, or constructor.
  • Always use the getModifiers() method to check the accessibility of the field, method, or constructor.

By following the above ways, you can avoid IllegalAccessException in Java.

java_lang_exceptions.htm
Advertisements