我有关于检查异常的另一个问题。我试图回答下面的问题。在我的尝试下方是原始问题和代码。如果我是对的,你能否告诉我,或者我可以如何改变我的尝试,使其正确。最诚挚的问候检查成员资格的方法 - 检查异常
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){}
}
checkMembership方法是Membership类的一部分。它的目的是 用于验证它作为参数传递的memberId,然后尝试在成员列表中找到它 。如果找到memberId,则返回true,否则返回false。
public boolean checkMembership(MemberId memberId)
{
if (!validate(memberId)) {
// An exception must be thrown.
…
}
// Further details of the check membership method are omitted.
…
}
如果MEMBERID参数checkMembership无效那么 InvalidMemberIdException必须抛出。重写上面显示的 checkMembership方法的一部分,以显示如何完成此操作。请记住,这是一个检查过的异常。您必须包含符合良好文体惯例的详细javadoc 评论。
为什么你在另一个方法声明中有方法声明?而且,为什么抛出一个异常,为什么不把它返回false呢? – bigblind 2011-05-16 18:57:35