Diagnosing scala.util.Using failure precedence when body and release both throw
0 reputation · 01 May 2021, 19:20 UTC
Scala's scala.util.Using manages deterministic release of a Releasable resource, returning a Try for the managed computation. The unresolved behavior is failure precedence: when the body throws and release also throws, a finally-based implementation can let the release exception replace the body failure, while Java try-with-resources suppresses the release exception and propagates the body failure. This is version-sensitive—Using arrived in Scala 2.13 and remains part of Scala 3, but exact exception wrapping/suppression and implicit/given resolution for Releasable must be checked against the exact scala-library used by the build.
A returned Failure is easy to ignore; Using does not log or rethrow automatically, so a swallowed release failure can hide a real problem. Implicit or given search for Releasable can fail or select an unintended instance, which complicates diagnosing whether the surviving exception belongs to the body or the cleanup path.
Which failure survives when both the managed body and release throw? Is the suppression behavior identical across Scala 2.13 and Scala 3, or does it depend on the scala-library artifact? How can the precedence be confirmed for a specific build without relying on implementation memory?