This lib hase zero dependencies and can be used to throw checked exceptions without declaring this in your method's head throws
clause.
And without wrapping Exception
into a RuntimeException
. Basically you can throw any Exception
everywhere without catching them, happy throwing :-D
unchecked(Exception)
rethrows any exception as unchecked exception, without wrapping exception.
throw unchecked(checkedException);
unchecked(LambdaFunction)
returns result or rethrows exception from lambda function as unchecked exception, without wrapping exception.
return unchecked(() -> methodThrowingCheckedException())
import static me.qoomon.UncheckedExceptions.*;
public class Example {
void example() {
URL url;
// code polition with try catch block
try {
url = new URL("https:/www.example.org");
} catch (MalformedURLException e) {
// ugly exception wrapping
throw RuntimeException(e);
}
System.out(url);
}
}
import static me.qoomon.UncheckedExceptions.*;
public class Example {
void example() {
// get rid of code polition with try catch block and exception wrapping
URL url = unchecked(() -> new URL("https:/www.example.org"));
System.out(url);
}
}
import static me.qoomon.UncheckedExceptions.*;
public class Example {
void example() {
Stream.of("https:/www.example.org")
.map(url -> {
// code polition with try catch block
try {
return new URL(url);
} catch (MalformedURLException e) {
// ugly exception wrapping
throw new RuntimeException(e);
}
});
}
}
import static me.qoomon.UncheckedExceptions.*;
public class Example {
void example() {
Stream.of("https:/www.example.org")
// get rid of code polition with try catch block and exception wrapping
.map(url -> unchecked(() -> new URL(url)));
}
}