Java 7 try-with-resources makes sure no exceptions are lost in case close
throws while another exception is already bubbling from the block's body. This is done by attaching the latter exception to the former as suppressed. Suppressed exceptions are made visible in printStackTrace
making it easier to diagnose complex problems involving multiple failures.
It seems with-open
doesn't record suppressed exceptions. close
-ing is handled in the finally
block, and if the stack has been already unwinding any exceptions thrown by close
will override.
On top of that, clojure.stacktrace
seems to ignore the existence of suppressed exceptions entirely.
Is there a reason not to support suppressed exceptions? Or is this just a legacy of pre-Java-7 implementation? Would you consider adding the support in?
Here's one implementation of with-open
supporting suppression: https://gist.github.com/mszajna/0ccbdfbd10927ff5d9517aaeb319952a
It forgoes of the finally block, closing the resource outside the try block in the happy case. In the unhappy case, the catch block does the suppression recording and makes sure the original exception propagates up.
I can imagine another implementation could make use of piece of state to record original exception and keep the finally block perhaps.
I don't have a proposal for clojure.stacktrace
at this stage. I'm not sure whether this should be a new function or should the suppressed exceptions be included in print-cause-trace
. On one hand, backward compatibility is nice, on the other, it would be great for existing tooling to take advantage of this improvement.