Throw From Finally (ARCH4155)
Ce contenu n’est pas encore disponible dans votre langue.
Key java.logging.throw-in-finally · Severity error · Category logging
Throwing from a finally block suppresses the in-flight exception from the try body. The original error is silently lost and the finally exception propagates in its place.
Incorrect
Section titled “Incorrect”try { process(data);} finally { resource.close(); // close() may throw}Correct
Section titled “Correct”try (var resource = open()) { process(data);} catch (IOException e) { log.error("Processing failed", e); throw e;}Manage resources with try-with-resources; wrap any throwing calls inside finally in their own try-catch.