简体   繁体   中英

Check membership method - checked exception

I have another question regarding checked exceptions. I have attempted to answer the question below.. Below my attempt is the original question and code. Could you let me know if I'm right or how I can change my attempt to make it correct. Kindest regards

public boolean checkMembership(MemberId memberId)
{
    // Firstly the method is tried to see if it works.
    try {
        public boolean checkMembership(MemberId memberId)
    }
    // If it does not work, then the exception is called
    catch (InvalidMemberIdException ex){}
}

The checkMembership method is part of the Membership class. Its purpose is to validate the memberId it is passed as its parameter and then try to find it in a list of members. It returns true if the memberId is found and false if not.

public boolean checkMembership(MemberId memberId)
{
    if (!validate(memberId)) {
        // An exception must be thrown.
        …
    }
    // Further details of the check membership method are omitted.
    …
}

If the memberId parameter to checkMembership is not valid then an InvalidMemberIdException must be thrown. Rewrite the part of the checkMembership method shown above to show how this would be done. Remember, this is a checked exception. You must include detailed javadoc comments for the method that conform to good stylistic conventions.

just add a

throw new InvalidMemberIdException("the id was invalid");

and update the javadocs.

edit -- i noticed them method as written is calling itself recursively (within the try catch block). You probably dont want to do this. Also, in the catch block you don't want to do nothing ('swallowing the exception' is usually bad). Put a log in there or something, or a comment that you are intentionally not doing anything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM