Java – possible null pointer exceptions for autoconceable idioms

Consider the following try with resources blocks:

try (Foo foo = getAFoo()) {

}

For implementing Java Some classes of lang. autoclosable foo

If getafoo () returns null, a null pointer exception will be thrown on the closing brace (due to the runtime attempt to call close)?

Solution

According to this Oracle blog:

This means that you can close any empty resource in the try block without throwing an error (the same is true when the program automatically attempts to close the resource at the end of the attempt)

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
分享
二维码
< <上一篇
下一篇>>