繁体   English   中英

Java正向流程和负向流程异常处理

[英]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.

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