Skip to content
develop
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 

jdeferthrow

Maven Central Maven Central (snapshot) Codecov

jdeferthrow

JVM Platform Status
OpenJDK LTS Linux Build (OpenJDK LTS, Linux)
OpenJDK Current Linux Build (OpenJDK Current, Linux)
OpenJDK Current Windows Build (OpenJDK Current, Windows)

The jdeferthrow package implements a trivial API for combining multiple exceptions over a series of statements.

Usage

final var tracker = new ExceptionTracker<IOException>();

try {
  doIO1();
} catch (IOException e1) {
  tracker.addException(e1);
}

try {
  doIO2();
} catch (IOException e2) {
  tracker.addException(e2);
}

try {
  doIO3();
} catch (IOException e3) {
  tracker.addException(e3);
}

tracker.throwIfNecessary();

The above code will execute doIO1, doIO2, and doIO3, catching each exception if any are raised. The throwIfNecessary method will throw whichever of e1, e2, or e3 was caught first, with any/all of the other two exceptions added to the thrown exception as a suppressed exception.

Concretely, if all of doIO1, doIO2, and doIO3 throw exceptions, the throwIfNecessary method will throw e1 with e2 and e3 added to e1 as suppressed exceptions.

This effectively allows for accumulating exceptions over a range of statements and then throwing an exception at the end that contains all of the exceptions that were thrown.

If addException was not called, throwIfNecessary does nothing.