Unchecked Exception
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
Methods
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())
Usage Examples
Try-Catch Block
Regular Code
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);
}
}
Unchecked Exception Code
import static me.qoomon.UncheckedExceptions.*;
public class Example {
void example() {
// get rid of code polition with try catch block
// and ugly exception wrapping
URL url = unchecked(() -> new URL("https:/www.example.org"));
System.out(url);
}
}
Stream
Regular Code
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);
}
});
}
}
Unchecked Exception Code
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 ugly exception wrapping
.map(url -> unchecked(() -> new URL(url)));
}
}