-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.in
46 lines (34 loc) · 1.15 KB
/
README.in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
### Description
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 either 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.