[英]Check membership method - checked exception
關於檢查異常,我還有另一個問題。 我試圖回答以下問題。我的嘗試下面是原始問題和代碼。 您能否讓我知道我是否正確,或如何更改我的嘗試以使其正確。 最親切的問候
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.
…
}
如果checkMembership的memberId參數無效,則必須引發InvalidMemberIdException。 重寫上面顯示的checkMembership方法的一部分,以顯示該操作。 請記住,這是一個檢查的異常。 您必須為該方法包括符合良好風格慣例的詳細javadoc注釋。
只需添加一個
throw new InvalidMemberIdException("the id was invalid");
並更新javadocs。
編輯-我注意到他們的方法是書面的遞歸調用自己(在try catch塊內)。 您可能不想這樣做。 另外,在catch塊中,您什么也不想做(“吞下異常”通常是不好的)。 在此處放置日志或其他內容,或添加注釋,以防止您故意不進行任何操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.