Java - EnumConstantNotPresentException



The Java EnumConstantNotPresentException is thrown when an attempt is made to access an enum constant that is not present in the enum. The EnumConstantNotPresentException is a runtime exception that is thrown when you try to access an enum constant that is not present in the enum.

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

  • When the user tries to access an enum constant that is not present in the enum, JVM throws an EnumConstantNotPresentException.

Constructors of EnumConstantNotPresentException

There are two constructors of EnumConstantNotPresentException class:

  • EnumConstantNotPresentException(Class<? extends Enum> enumType, String constantName): This constructor is used to create an EnumConstantNotPresentException object with the enum type and the constant name.
  • EnumConstantNotPresentException(String message): This constructor is used to create an EnumConstantNotPresentException object with a message.

Methods of EnumConstantNotPresentException

There are some methods of EnumConstantNotPresentException 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 EnumConstantNotPresentException

In this example, we are trying to access an enum constant that is not present in the enum, so JVM will throw an EnumConstantNotPresentException.

public class EnumConstantNotPresentExceptionExample {

   public enum Day {
      SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
   }

   public static void main(String[] args) {
      Day day = Day.valueOf("SUNDAY");
      System.out.println(day);
      Day day1 = Day.valueOf("SUNDAY1");
      System.out.println(day1);
   }
}

Output

Following is the output of the above code:

SUNDAY
Exception in thread "main" java.lang.EnumConstantNotPresentException: EnumConstantNotPresentExceptionExample$Day.SUNDAY1
    at EnumConstantNotPresentExceptionExample.main(EnumConstantNotPresentExceptionExample.java:10)

As you can see in the output, JVM throws an EnumConstantNotPresentException because we are trying to access an enum constant that is not present in the enum.

Handling EnumConstantNotPresentException

In this example, we are trying to access an enum constant that is not present in the enum, so JVM will throw an EnumConstantNotPresentException. We are handling this exception using try-catch block.

public class EnumConstantNotPresentExceptionExample {

   public enum Day {
      SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
   }

   public static void main(String[] args) {
      try {
         Day day1 = Day.valueOf("SUNDAY1");
         System.out.println(day1);
      } catch(EnumConstantNotPresentException e) {
         System.out.println("Enum constant is not present");
      }
   }
}

Output

Following is the output of the above code:

SUNDAY
Enum constant is not present

As you can see in the output, we are handling the EnumConstantNotPresentException using try-catch block.

Throwing EnumConstantNotPresentException explicitly

There is no way to throw an EnumConstantNotPresentException explicitly because it is a runtime exception and it is thrown by JVM when you try to access an enum constant that is not present in the enum.

But you can create a custom exception that extends the EnumConstantNotPresentException class and throw it explicitly.

Example of throwing EnumConstantNotPresentException explicitly

In this example, we are creating a custom exception that extends the EnumConstantNotPresentException class and throwing it explicitly.

public class EnumConstantNotPresentExceptionExample {
   public static void main(String[] args) {
      try {
         throw new CustomEnumConstantNotPresentException("Custom Enum Constant Not Present Exception");
      } catch(CustomEnumConstantNotPresentException e) {
         System.out.println(e.getMessage());
      }
   }
}

class CustomEnumConstantNotPresentException extends EnumConstantNotPresentException {

   public CustomEnumConstantNotPresentException(String message) {
      super(message);
   }
}

Output

Following is the output of the above code:

Custom Enum Constant Not Present Exception

As you can see in the output, we are throwing a custom exception that extends the EnumConstantNotPresentException class and handling it using a try-catch block.

How to avoid EnumConstantNotPresentException?

Here are some ways to avoid EnumConstantNotPresentException in Java:

  • Always check the enum constant before accessing it.
  • Always use the try-catch block to handle the EnumConstantNotPresentException.
  • Always use the valueOf() method to access the enum constant.

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

Example of avoiding EnumConstantNotPresentException

Let's see an example to avoid EnumConstantNotPresentException:

public class EnumConstantNotPresentExceptionExample {

   public enum Day {
      SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
   }

   public static void main(String[] args) {
      Day day = Day.SUNDAY;
      System.out.println(day);
      if(day.equals(Day.SUNDAY)) {
         System.out.println("Enum constant is present");
      } else {
         System.out.println("Enum constant is not present");
      }
   }
}

Output

Following is the output of the above code:

SUNDAY
Enum constant is present

As you can see in the output, we are avoiding the EnumConstantNotPresentException by checking the enum constant before accessing it.

java_lang_exceptions.htm
Advertisements