'Java Positive flow and negative flow exception handling

I am writing a Java code to fetch the object information by passing the object id. My case will be like this: 1. Its not necessary that all Object ID's have information which means returns null. 2. But If the ID has a information, then I will return the object. So I am doing null check here.

If I write a else condition which throws the KalturaApiException, then its throwing the exception saying EntryId not found and stopping the execution there. My problem is it should continue with the positive flow and should log all the ids with no information. How to handle this scenario and how to catch this exception. Please help me resolving this. Thanks in advance.

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();
}

Inside getMedia method:

try {
        entryInfo = mediaService.get(entryId);
        if (entryInfo != null) {
            return entryInfo;
        }
    } catch (KalturaApiException e) {
        e.getMessage();
    }
    return entryInfo;


Solution 1:[1]

If i understand you right, mediaService.get also throw KalturaApiException. I think you don't need to worry about it. So inside getMedia you can just

return mediaService.get(entryId);

null also an object and you do null check in the other method. Exception also will be catched in your first method. (don't forget sign 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)
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 prsmax