I'd like to find a way to hint SonarQube so that it would not warn for some pieces of code.
Example1: "empty statement block can be removed" -- actually, it cannot be removed:
static void closeNX (java.io.FileInputStream is) {
try {
is.close ();
} catch (Exception e) {} <Warning comes here>
}
(Edit: this method is called when is.read
or is.write
has already thrown an Exception, so any more Exception from is.close
is not needed.)
Example2: finalize
is deprecated, as the memory will be eventually released even if the user of your library forget to call close
/dispose
-- but native resources (opened files, sockets, IPC, whatever) won't be released, that's why for example java.io.FileInputStream
does have finalize
method:
protected void finalize() throws IOException {
if ((fd != null) && (fd != FileDescriptor.in)) {
close();
}
}
(Edit: even having finalize
doesn't warrant that native resources will be released if the user doesn't call close
/dispose
; but non-having finalize
does warrant that native resources won't be released if the user doesn't call close
/dispose
)