cactoos: Handle Exceptions in object oriented style
I’ve created a small library that represents try/catch/finally statements in a form of reusable objects and I thought it would be nice to include it in cactoos.
One example of using objects instead of statements could be refactoring IoCheckedScalar:
public T value() throws IOException {
try {
return this.origin.value();
// @checkstyle IllegalCatchCheck (1 line)
} catch (final IOException | RuntimeException ex) {
throw ex;
} catch (final InterruptedException ex) {
Thread.currentThread().interrupt();
throw new IOException(ex);
// @checkstyle IllegalCatchCheck (1 line)
} catch (final Exception ex) {
throw new IOException(ex);
}
}
To this:
public T value() throws IOException {
return
new Try(
new Catch(
InterruptedException.class,
exp -> Thread.currentThread().interrupt()
)
).with(new Throws<>(IOException::new)
).exec(this.origin::value);
}
There are more examples and details here. (The library follows OOP style from cactoos) If you find this usable and appropriate to be included in cactoos I can create a pull request and merge it in a new package (eg. exceptions).
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 37 (35 by maintainers)
This issue was resolved in following issues: #735 #736 #759 #821
I give up from
FinalizedScalarandCloseableScalarsince the usability of this classes is questionable.@yegor256 How about we implement “try with resources” functionality instead of finally:
Usage:
WDYT?
@Vatavuk You could extend this with classes for
if/then/elseandfor/forEachand you almost have created a complete class based language.