Q:

When does java.lang.EnumConstantNotPresentException gets thrown?

As per java API EnumConstantNotPresentException gets thrown when an application tries to access an enum constant by name and the enum type contains no constant with the specified name.
Thus I decided to come up with a scenario where java.lang.EnumConstantNotPresentException will be thrown, so I wrote following three classes.
MyEnum class:
package my.enumtest;

enum MyEnum {
    A, B, C;
}

MyEnumTest1 class:
package my.enumtest;

    import my.enumtest.MyEnum;

    class MyEnumTest1 {

        public static void main(String [] args) {
            System.out.println(MyEnum.A);        
        }        
    }

MyEnumTest2 class:
package my.enumtest;

import my.enumtest.MyEnum;

class MyEnumTest2 {

    public static void main(String [] args) {
        System.out.println(MyEnum.valueOf("A"));        
    }
}

I compiled all three and then I changed the MyEnum class to drop constant 'A' and recompiled it:
package my.enumtest;

enum MyEnum {
    B, C;
}

On execution of MyEnumTest1 with new MyEnum class, I got following exception:

Exception in thread "main"
  java.lang.NoSuchFieldError: A
          at my.enumtest.MyEnumTest1.main(MyEnumTest1.java:8)

On execution of MyEnumTest2 with new MyEnum class, I got following exception:

Exception in thread "main" java.lang.IllegalArgumentException: No enum
  constant my.enumtest.MyEnum.A
          at java.lang.Enum.valueOf(Unknown Source)
          at my.enumtest.MyEnum.valueOf(MyEnum.java:3)
          at my.enumtest.MyEnumTest2.main(MyEnumTest2.java:8)

As you can see in none of the cases I got EnumConstantNotPresentException, so can someone provide me usage of EnumConstantNotPresentException class?
P.S. I know that this exception can be thrown by the API used to read annotations reflectively but I am looking for more obvious (simpler) scenario.

A:

First thing you should do if you want to know when the specific exception is thrown is to read the documentation as JB Nizet mentioned. It says:

Thrown when an application tries to access an enum constant by name and the enum type contains no constant with the specified name. This exception can be thrown by the API used to read annotations reflectively.

The link follows to AnnotationElement which documentation says:

Similarly, attempting to read an enum-valued member will result in a EnumConstantNotPresentException if the enum constant in the annotation is no longer present in the enum type. 

This is enough to craft an example.
Create the following classes:
// TestEnum.java
public enum TestEnum {
    A, B, C;
}

// TestAnnotation.java
import java.lang.annotation.*;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnnotation {
    TestEnum value();
}

// TestClass.java
@TestAnnotation(TestEnum.C)
public class TestClass {

}

// ReadAnnotation.java
public class ReadAnnotation {
    public static void main(String[] args) {
        System.out.println(TestClass.class.getAnnotation(TestAnnotation.class).value());
    }
}

Compile everything and run ReadAnnotation. You will got C.
Now remove the C from the TestEnum and recompile only TestEnum class preserving other classes as is. If you launch ReadAnnotation now, you will get:
Exception in thread "main" java.lang.EnumConstantNotPresentException: TestEnum.C
    at sun.reflect.annotation.EnumConstantNotPresentExceptionProxy.generateException(Unknown Source)
    at sun.reflect.annotation.AnnotationInvocationHandler.invoke(Unknown Source)
    at com.sun.proxy.$Proxy1.value(Unknown Source)
    at ReadAnnotation.main(ReadAnnotation.java:4)

If you wonder whether it can be thrown by anything else, you may scan the JDK sources for this exception name. I did not found any other mentions of this exception, so seems that reflection is the only possible case.

