Search before asking
Motivation
WriteContextImpl#finish(boolean) runs its steps one after another and collects failures into a single throwable. Each catch overwrites it, so when more than one step fails, the caller only sees the last failure (code). Shortened:
Throwable throwable = null;
...
try {
workbook.write(outputStream); // 1. fails
workbook.close();
} catch (Throwable t) {
throwable = t; // recorded
}
...
try {
outputStream.close(); // 2. fails too, because the stream is already broken
} catch (Throwable t) {
throwable = t; // overwrites 1.
}
...
if (throwable != null) {
throw new ExcelGenerateException("Can not close IO.", throwable); // cause = 2.
}
Solution
The first catch can't overwrite anything. In the five after it, keep the exception only if none has been recorded yet:
} catch (Throwable t) {
if (throwable == null) {
throwable = t;
}
}
Alternatives
Attach later failures with addSuppressed. Nothing is lost, but each catch gets a helper call, and later failures are usually consequences of the first anyway.
Anything else?
For background: when this code was first written, each of these catch blocks threw straight away, so the first failure was the one reported. alibaba/easyexcel@2c8918be changed them to save the exception and carry on, so the later close steps would still run.
Are you willing to submit a PR?
Search before asking
Motivation
WriteContextImpl#finish(boolean)runs its steps one after another and collects failures into a singlethrowable. Eachcatchoverwrites it, so when more than one step fails, the caller only sees the last failure (code). Shortened:Solution
The first
catchcan't overwrite anything. In the five after it, keep the exception only if none has been recorded yet:Alternatives
Attach later failures with
addSuppressed. Nothing is lost, but eachcatchgets a helper call, and later failures are usually consequences of the first anyway.Anything else?
For background: when this code was first written, each of these
catchblocks threw straight away, so the first failure was the one reported. alibaba/easyexcel@2c8918be changed them to save the exception and carry on, so the later close steps would still run.Are you willing to submit a PR?