An exception is thrown from a finally block, replacing any exception being thrown
What it finds
A throw in a finally block replaces whatever the try block threw: the original exception, usually the more telling one, is lost. A failure of cleanup can be added to it with addSuppressed instead.
A throw inside a try statement within the finally block that catches it is left alone.
From the rule's source, throw_in_finally.rs.
class Files { void copy(java.io.Closeable c) throws java.io.IOException { try { work(); } finally { if (c == null) { throw new IllegalStateException("no stream");tx0 reports: replaces any exception the try block threw } try { c.close(); } catch (java.io.IOException e) { throw new IllegalStateException(e);tx0 reports: replaces any exception } try { c.close(); } catch (RuntimeException e) { log(e); } Runnable r = () -> { throw new IllegalStateException(); }; } } void work() {} void log(Exception e) {}} Turn it on, off or suppress it
The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/java:throw-in-finally" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/java:throw-in-finally] reason="..."tx0 explain tx0/java:throw-in-finally shows the same in the terminal. Found a false positive? Report it.