Java – a try catch scenario – is it used correctly

I'm looking for a code base where the domain model consists of many nested member variables

Consider this situation

private static String getSomeStringRepresentation(A input) {
    String result = "";
    try {
         result = input.getTypeA().getTypeAInfo().get(0).getRepresentation();
    } catch (NullPointerException e) {
        Logger.logDebug(e.getMessage());
    }
    return result;
}

Any method call in this call chain may result in NullPointerException Is it correct to use the catch clause to handle it in this case? Is this a "can handle exceptions" situation?

edit

It's really ugly to check null four times Don't you think it's reasonable to capture NPE in this case?

The problem here is to call some methods on a possibly null object

Solution

Why not check for nulls instead of setting the catch block? Capturing NullPointerException is not considered a good practice

If catching null pointer exception is not a good practice,is catching exception a good one?

also

Is Catching a Null Pointer Exception a Code Smell?

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>