try-with-resources and null resource
tom.hawtin at oracle.com
tom.hawtin at oracle.com
Fri Jan 28 06:09:28 PST 2011
On 28/01/2011 06:54, Reinier Zwitserloot wrote:
> InputStream introTextStream1 = getClass().getResourceAsStream();
> if (introTextStream1 != null) {
> try (InputStream introTextStream2 = introTextStream1) {
> ...
> }
> }
So Class.getResourceAsStream is a poorly designed method. Wrap it in
something better:
public static @Nonnull InputStream openResource(
@Nonnull Class<?> clazz, /* @Nonnull ??? */ String name
) throws FileNotFoundException {
InputStream stream = clazz.getResourceAsStream(name);
if (stream == null) {
// Put back a replacement for the swallowed exception.
throw new FileNotFoundException(
name
);
}
return stream;
}
(You are probably going to have to deal with IOException anyway.
FileNotFoundException by analogy to 404 and 410 handling which may well
have caused the null, but there probably should be a mezzanine
IOException. Even with a quick look at the source, I can't see what is
supposed to happen for a null resource path.)
Tom
More information about the coin-dev
mailing list