[英]Java Positive flow and negative flow exception handling
我正在編寫Java代碼以通過傳遞對象ID來獲取對象信息。 我的情況將是這樣的:1.不必所有對象ID都有信息,這意味着返回null。 2.但是,如果ID有信息,那么我將返回該對象。 所以我在這里做空檢查。
如果我編寫了一個其他條件,該條件引發KalturaApiException,則該條件引發異常,表明未找到EntryId,並在那里停止執行。 我的問題是它應該繼續進行正向流動,並且應該記錄所有沒有任何信息的id。 如何處理這種情況以及如何捕獲此異常。 請幫我解決這個問題。 提前致謝。
try {
entryInfo = getMedia(entry);
if (entryInfo != null) {
//Here I am retrieving all the information from the object and setting to one more object.
}
}catch(KalturaApiException e){
e.getMessage();
}
里面的getMedia方法:
try {
entryInfo = mediaService.get(entryId);
if (entryInfo != null) {
return entryInfo;
}
} catch (KalturaApiException e) {
e.getMessage();
}
return entryInfo;
如果我理解正確,mediaService.get也會拋出KalturaApiException。 我認為您不必為此擔心。 因此,在getMedia內部,您可以
return mediaService.get(entryId);
將一個對象也設為null,然后在另一個方法中執行null檢查。 您的第一個方法也會捕獲異常。 (不要忘記將符號getMedia(long id) throw KalturaApiException {...}
)
try {
entryInfo = getMedia(entry);
if (entryInfo != null) {
// do something
}
}catch(KalturaApiException e){
e.getMessage(); //log information, it's better to use logger (log4j for example)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.