开发者

Check membership method - checked exception

开发者 https://www.devze.com 2023-03-06 02:43 出处:网络
I have another question regarding checked exceptions. I have attempted to answer the question below.. B开发者_如何学Pythonelow my attempt is the original question and code. Could you let me know if I\

I have another question regarding checked exceptions. I have attempted to answer the question below.. B开发者_如何学Pythonelow 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消