Skip to content

Comment on Lombok Saved My Ass

Comments

    OutputStream out = new BarOutputStream();
    in.transferTo(out);
    if (out != null) ...
Can someone explain how `out` could possibly be null here?

How it's written here it won't be null. However the general pattern is to check for null in the finally block since it may not be provable that the stream is non-null (e.g. created in a library call or similar). In fact the try-with-resources feature handles the null case as well.

In the article, the if check occurs in a finally clause. So the code would execute even if the constructor threw an exception. Hence the need to null-check it first.

But that just isn't true. Neither finally-block appends a try-block that initializes the stream that it null-checks.

If the `in` stream init throws, the entire try-block isn't run at all. If `out` stream init throws, the following try/finally block isn't run either.

Lombok's docs get this wrong too. Unless I'm going crazy.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.