uncheck is a very simple Java library aiming to ease the transformation of checked exceptions into runtime exceptions using lambda.
Wrapping a checked exception inside a runtime exception usually requires some boilerplate code:
try {
unsafeMethod();
} catch(Exception e) {
throw new RuntimeException(e);
}
This can even be more verbose when the unsafe call returns a value to process:
Object result;
try {
result = unsafeMethod();
} catch(Exception e) {
throw new RuntimeException(e);
}
process(result);
Using uncheck, you can improve the readability of your code by removing the boilerplate part of it:
uncheck(this::unsafeMethod);
It also nicely handles methods returning values:
Object result = uncheck(this::unsafeMethod);
process(result);
Note
Caught exceptions are processed this way:
- RuntimeException are just propagated as they are
- IOException are wrapped inside UncheckedIOException
- other exceptions are wrapped inside RuntimeException
Just add the following dependency in your pom.xml
:
<dependency>
<groupId>com.github.ylegat</groupId>
<artifactId>uncheck</artifactId>
<version>1.0</version>
</dependency>