Zum Inhalt springen

Throw From Finally (ARCH4155)

Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.

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.

try {
process(data);
} finally {
resource.close(); // close() may throw
}
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.