Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Unreported Exception arguments passed into enum's constructor

807589Jul 11 2008 — edited Jul 11 2008
Hello!

Hey, all! ^_^

I'm trying to pass an instance of Method into an enum constructor, but the compiler is telling me there will be a compile error because of an unreported exception.

Exception: "unreported exception java.lang.NoSuchMethodException; must be caught or declared to be thrown."

java version 6 update 5
public class Whatever{
public static enum PhoneType
    {
        MAIN_PHONE(Person.class.getDeclaredMethod("getPhone", new Class[0])),
        OTHER_PHONE(Person.class.getDeclaredMethod("getOtherPhone",  new Class[0])),
        CELL_HONE(Person.class.getDeclaredMethod("getCellPhone",  new Class[0]));
           
        private Method method;
        
        PhoneType(Method method)
        {
            this.method = method;
        }

        public Method getMethod()
        {
            return method;
        }

        public void setMethod(Method method)
        {
            this.method = method;
        }
    }// end enum PhoneType
}// end class whatever
I tried this:
MAIN_PHONE(try{Person.class.getDeclaredMethod("getPhone", new Class[0]);}catch(Exception e){}),
Yeah, that made compiler go nuts..

and this:
try{
        MAIN_PHONE(Person.class.getDeclaredMethod("getPhone", new Class[0])),
        OTHER_PHONE(Person.class.getDeclaredMethod("getOtherPhone",  new Class[0])),
        CELL_HONE(Person.class.getDeclaredMethod("getCellPhone",  new Class[0]));
        }
        catch(Exception e){}
Even tried putting "throws Exception" on after the enum declaration and constructor. (/me == desperate ><) No workie.

Is there a way to solve this problem? If so, how?

Thanks!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 8 2008
Added on Jul 11 2008
4 comments
305 views