Java 8 Lambda function that throws exception? - java
I know how to create a reference to a method that has a String parameter and returns an int, it's:
Function<String, Integer>
However, this doesn't work if the function throws an exception, say it's defined as:
Integer myMethod(String s) throws IOException
How would I define this reference?
You'll need to do one of the following.
If it's your code, then define your own functional interface that declares the checked exception:
#FunctionalInterface
public interface CheckedFunction<T, R> {
R apply(T t) throws IOException;
}
and use it:
void foo (CheckedFunction f) { ... }
Otherwise, wrap Integer myMethod(String s) in a method that doesn't declare a checked exception:
public Integer myWrappedMethod(String s) {
try {
return myMethod(s);
}
catch(IOException e) {
throw new UncheckedIOException(e);
}
}
and then:
Function<String, Integer> f = (String t) -> myWrappedMethod(t);
or:
Function<String, Integer> f =
(String t) -> {
try {
return myMethod(t);
}
catch(IOException e) {
throw new UncheckedIOException(e);
}
};
You can actually extend Consumer (and Function etc.) with a new interface that handles exceptions -- using Java 8's default methods!
Consider this interface (extends Consumer):
#FunctionalInterface
public interface ThrowingConsumer<T> extends Consumer<T> {
#Override
default void accept(final T elem) {
try {
acceptThrows(elem);
} catch (final Exception e) {
// Implement your own exception handling logic here..
// For example:
System.out.println("handling an exception...");
// Or ...
throw new RuntimeException(e);
}
}
void acceptThrows(T elem) throws Exception;
}
Then, for example, if you have a list:
final List<String> list = Arrays.asList("A", "B", "C");
If you want to consume it (eg. with forEach) with some code that throws exceptions, you would traditionally have set up a try/catch block:
final Consumer<String> consumer = aps -> {
try {
// maybe some other code here...
throw new Exception("asdas");
} catch (final Exception ex) {
System.out.println("handling an exception...");
}
};
list.forEach(consumer);
But with this new interface, you can instantiate it with a lambda expression and the compiler will not complain:
final ThrowingConsumer<String> throwingConsumer = aps -> {
// maybe some other code here...
throw new Exception("asdas");
};
list.forEach(throwingConsumer);
Or even just cast it to be more succinct!:
list.forEach((ThrowingConsumer<String>) aps -> {
// maybe some other code here...
throw new Exception("asda");
});
Update
Looks like there's a very nice utility library part of Durian called Errors which can be used to solve this problem with a lot more flexibility. For example, in my implementation above I've explicitly defined the error handling policy (System.out... or throw RuntimeException), whereas Durian's Errors allow you to apply a policy on the fly via a large suite of utility methods. Thanks for sharing it, #NedTwigg!.
Sample usage:
list.forEach(Errors.rethrow().wrap(c -> somethingThatThrows(c)));
I think Durian's Errors class combines many of the pros of the various suggestions above.
Wrap a throwing function to a standard Java 8 functional interface.
Easily specify various policies for handling errors
When wrapping a method that returns a value, there is an important distinction between specifying a default value or rethrowing a RuntimeException.
Throwing versions of Java 8's functional interfaces
Similar to fge's answer
Standard interfaces for throwing specific exceptions
Which addresses Zoltán's concern
To include Durian in your project, you can either:
grab it from jcenter or maven central at com.diffplug.durian:durian:3.3.0
or just copy paste just two small classes into your code: Throwing.java and Errors.java
This is not specific to Java 8. You are trying to compile something equivalent to:
interface I {
void m();
}
class C implements I {
public void m() throws Exception {} //can't compile
}
Disclaimer: I haven't used Java 8 yet, only read about it.
Function<String, Integer> doesn't throw IOException, so you can't put any code in it that throws IOException. If you're calling a method that expects a Function<String, Integer>, then the lambda that you pass to that method can't throw IOException, period. You can either write a lambda like this (I think this is the lambda syntax, not sure):
(String s) -> {
try {
return myMethod(s);
} catch (IOException ex) {
throw new RuntimeException(ex);
// (Or do something else with it...)
}
}
Or, if the method you're passing the lambda to is one you wrote yourself, you can define a new functional interface and use that as the parameter type instead of Function<String, Integer>:
public interface FunctionThatThrowsIOException<I, O> {
O apply(I input) throws IOException;
}
If you don't mind to use a 3rd party lib (Vavr) you could write
CheckedFunction1<String, Integer> f = this::myMethod;
It also has the so-called Try monad which handles errors:
Try(() -> f.apply("test")) // results in a Success(Integer) or Failure(Throwable)
.map(i -> ...) // only executed on Success
...
Please read more here.
Disclaimer: I'm the creator of Vavr.
Sneaky throw idiom enables bypassing CheckedException of Lambda expression. Wrapping a CheckedException in a RuntimeException is not good for strict error handling.
It can be used as a Consumer function used in a Java collection.
Here is a simple and improved version of jib's answer.
import static Throwing.rethrow;
#Test
public void testRethrow() {
thrown.expect(IOException.class);
thrown.expectMessage("i=3");
Arrays.asList(1, 2, 3).forEach(rethrow(e -> {
int i = e.intValue();
if (i == 3) {
throw new IOException("i=" + i);
}
}));
}
This just wrapps the lambda in a rethrow. It makes CheckedException rethrow any Exception that was thrown in your lambda.
public final class Throwing {
private Throwing() {}
#Nonnull
public static <T> Consumer<T> rethrow(#Nonnull final ThrowingConsumer<T> consumer) {
return consumer;
}
/**
* The compiler sees the signature with the throws T inferred to a RuntimeException type, so it
* allows the unchecked exception to propagate.
*
* http://www.baeldung.com/java-sneaky-throws
*/
#SuppressWarnings("unchecked")
#Nonnull
public static <E extends Throwable> void sneakyThrow(#Nonnull Throwable ex) throws E {
throw (E) ex;
}
}
Find a complete code and unit tests here.
You could however create your own FunctionalInterface that throws as below..
#FunctionalInterface
public interface UseInstance<T, X extends Throwable> {
void accept(T instance) throws X;
}
then implement it using Lambdas or references as shown below.
import java.io.FileWriter;
import java.io.IOException;
//lambda expressions and the execute around method (EAM) pattern to
//manage resources
public class FileWriterEAM {
private final FileWriter writer;
private FileWriterEAM(final String fileName) throws IOException {
writer = new FileWriter(fileName);
}
private void close() throws IOException {
System.out.println("close called automatically...");
writer.close();
}
public void writeStuff(final String message) throws IOException {
writer.write(message);
}
//...
public static void use(final String fileName, final UseInstance<FileWriterEAM, IOException> block) throws IOException {
final FileWriterEAM writerEAM = new FileWriterEAM(fileName);
try {
block.accept(writerEAM);
} finally {
writerEAM.close();
}
}
public static void main(final String[] args) throws IOException {
FileWriterEAM.use("eam.txt", writerEAM -> writerEAM.writeStuff("sweet"));
FileWriterEAM.use("eam2.txt", writerEAM -> {
writerEAM.writeStuff("how");
writerEAM.writeStuff("sweet");
});
FileWriterEAM.use("eam3.txt", FileWriterEAM::writeIt);
}
void writeIt() throws IOException{
this.writeStuff("How ");
this.writeStuff("sweet ");
this.writeStuff("it is");
}
}
You can use unthrow wrapper
Function<String, Integer> func1 = s -> Unthrow.wrap(() -> myMethod(s));
or
Function<String, Integer> func2 = s1 -> Unthrow.wrap((s2) -> myMethod(s2), s1);
You can.
Extending #marcg 's UtilException and adding generic <E extends Exception> where necessary: this way, the compiler will force you again to add throw clauses and everything's as if you could throw checked exceptions natively on java 8's streams.
public final class LambdaExceptionUtil {
#FunctionalInterface
public interface Function_WithExceptions<T, R, E extends Exception> {
R apply(T t) throws E;
}
/**
* .map(rethrowFunction(name -> Class.forName(name))) or .map(rethrowFunction(Class::forName))
*/
public static <T, R, E extends Exception> Function<T, R> rethrowFunction(Function_WithExceptions<T, R, E> function) throws E {
return t -> {
try {
return function.apply(t);
} catch (Exception exception) {
throwActualException(exception);
return null;
}
};
}
#SuppressWarnings("unchecked")
private static <E extends Exception> void throwActualException(Exception exception) throws E {
throw (E) exception;
}
}
public class LambdaExceptionUtilTest {
#Test
public void testFunction() throws MyTestException {
List<Integer> sizes = Stream.of("ciao", "hello").<Integer>map(rethrowFunction(s -> transform(s))).collect(toList());
assertEquals(2, sizes.size());
assertEquals(4, sizes.get(0).intValue());
assertEquals(5, sizes.get(1).intValue());
}
private Integer transform(String value) throws MyTestException {
if(value==null) {
throw new MyTestException();
}
return value.length();
}
private static class MyTestException extends Exception { }
}
I had this problem with Class.forName and Class.newInstance inside a lambda, so I just did:
public Object uncheckedNewInstanceForName (String name) {
try {
return Class.forName(name).newInstance();
}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
Inside the lambda, instead of calling Class.forName("myClass").newInstance() I just called uncheckedNewInstanceForName ("myClass")
Create a custom return type that will propagate the checked exception. This is an alternative to creating a new interface that mirrors the existing functional interface with the slight modification of a "throws exception" on the functional interface's method.
Definition
CheckedValueSupplier
public static interface CheckedValueSupplier<V> {
public V get () throws Exception;
}
CheckedValue
public class CheckedValue<V> {
private final V v;
private final Optional<Exception> opt;
public Value (V v) {
this.v = v;
}
public Value (Exception e) {
this.opt = Optional.of(e);
}
public V get () throws Exception {
if (opt.isPresent()) {
throw opt.get();
}
return v;
}
public Optional<Exception> getException () {
return opt;
}
public static <T> CheckedValue<T> returns (T t) {
return new CheckedValue<T>(t);
}
public static <T> CheckedValue<T> rethrows (Exception e) {
return new CheckedValue<T>(e);
}
public static <V> CheckedValue<V> from (CheckedValueSupplier<V> sup) {
try {
return CheckedValue.returns(sup.get());
} catch (Exception e) {
return Result.rethrows(e);
}
}
public static <V> CheckedValue<V> escalates (CheckedValueSupplier<V> sup) {
try {
return CheckedValue.returns(sup.get());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Usage
// Don't use this pattern with FileReader, it's meant to be an
// example. FileReader is a Closeable resource and as such should
// be managed in a try-with-resources block or in another safe
// manner that will make sure it is closed properly.
// This will not compile as the FileReader constructor throws
// an IOException.
Function<String, FileReader> sToFr =
(fn) -> new FileReader(Paths.get(fn).toFile());
// Alternative, this will compile.
Function<String, CheckedValue<FileReader>> sToFr = (fn) -> {
return CheckedValue.from (
() -> new FileReader(Paths.get("/home/" + f).toFile()));
};
// Single record usage
// The call to get() will propagate the checked exception if it exists.
FileReader readMe = pToFr.apply("/home/README").get();
// List of records usage
List<String> paths = ...; //a list of paths to files
Collection<CheckedValue<FileReader>> frs =
paths.stream().map(pToFr).collect(Collectors.toList());
// Find out if creation of a file reader failed.
boolean anyErrors = frs.stream()
.filter(f -> f.getException().isPresent())
.findAny().isPresent();
What's going on?
A single functional interface that throws a checked exception is created (CheckedValueSupplier). This will be the only functional interface which allows checked exceptions. All other functional interfaces will leverage the CheckedValueSupplier to wrap any code that throws a checked exception.
The CheckedValue class will hold the result of executing any logic that throws a checked exception. This prevents propagation of a checked exception until the point at which code attempts to access the value that an instance of CheckedValue contains.
The problems with this approach.
We are now throwing "Exception" effectively hiding the specific type originally thrown.
We are unaware that an exception occurred until CheckedValue#get() is called.
Consumer et al
Some functional interfaces (Consumer for example) must be handled in a different manner as they don't provide a return value.
Function in lieu of Consumer
One approach is to use a function instead of a consumer, which applies when handling streams.
List<String> lst = Lists.newArrayList();
// won't compile
lst.stream().forEach(e -> throwyMethod(e));
// compiles
lst.stream()
.map(e -> CheckedValueSupplier.from(
() -> {throwyMethod(e); return e;}))
.filter(v -> v.getException().isPresent()); //this example may not actually run due to lazy stream behavior
Escalate
Alternatively, you can always escalate to a RuntimeException. There are other answers that cover escalation of a checked exception from within a Consumer.
Don't consume.
Just avoid functional interfaces all together and use a good-ole-fashioned for loop.
Another solution using a Function wrapper would be to return either an instance of a wrapper of your result, say Success, if everything went well, either an instance of, say Failure.
Some code to clarify things :
public interface ThrowableFunction<A, B> {
B apply(A a) throws Exception;
}
public abstract class Try<A> {
public static boolean isSuccess(Try tryy) {
return tryy instanceof Success;
}
public static <A, B> Function<A, Try<B>> tryOf(ThrowableFunction<A, B> function) {
return a -> {
try {
B result = function.apply(a);
return new Success<B>(result);
} catch (Exception e) {
return new Failure<>(e);
}
};
}
public abstract boolean isSuccess();
public boolean isError() {
return !isSuccess();
}
public abstract A getResult();
public abstract Exception getError();
}
public class Success<A> extends Try<A> {
private final A result;
public Success(A result) {
this.result = result;
}
#Override
public boolean isSuccess() {
return true;
}
#Override
public A getResult() {
return result;
}
#Override
public Exception getError() {
return new UnsupportedOperationException();
}
#Override
public boolean equals(Object that) {
if(!(that instanceof Success)) {
return false;
}
return Objects.equal(result, ((Success) that).getResult());
}
}
public class Failure<A> extends Try<A> {
private final Exception exception;
public Failure(Exception exception) {
this.exception = exception;
}
#Override
public boolean isSuccess() {
return false;
}
#Override
public A getResult() {
throw new UnsupportedOperationException();
}
#Override
public Exception getError() {
return exception;
}
}
A simple use case :
List<Try<Integer>> result = Lists.newArrayList(1, 2, 3).stream().
map(Try.<Integer, Integer>tryOf(i -> someMethodThrowingAnException(i))).
collect(Collectors.toList());
This problem has been bothering me as well; this is why I have created this project.
With it you can do:
final ThrowingFunction<String, Integer> f = yourMethodReferenceHere;
There are a totla of 39 interfaces defined by the JDK which have such a Throwing equivalent; those are all #FunctionalInterfaces used in streams (the base Stream but also IntStream, LongStream and DoubleStream).
And as each of them extend their non throwing counterpart, you can directly use them in lambdas as well:
myStringStream.map(f) // <-- works
The default behavior is that when your throwing lambda throws a checked exception, a ThrownByLambdaException is thrown with the checked exception as the cause. You can therefore capture that and get the cause.
Other features are available as well.
There are a lot of great responses already posted here. Just attempting to solve the problem with a different perspective. Its just my 2 cents, please correct me if I am wrong somewhere.
Throws clause in FunctionalInterface is not a good idea
I think this is probably not a good idea to enforce throws IOException because of following reasons
This looks to me like an anti-pattern to Stream/Lambda. The whole idea is that the caller will decide what code to provide and how to handle the exception. In many scenarios, the IOException might not be applicable for the client. For example, if the client is getting value from cache/memory instead of performing actual I/O.
Also, the exceptions handling in streams becomes really hideous. For example, here is my code will look like if I use your API
acceptMyMethod(s -> {
try {
Integer i = doSomeOperation(s);
return i;
} catch (IOException e) {
// try catch block because of throws clause
// in functional method, even though doSomeOperation
// might not be throwing any exception at all.
e.printStackTrace();
}
return null;
});
Ugly isn't it? Moreover, as I mentioned in my first point, that the doSomeOperation method may or may not be throwing IOException (depending on the implementation of the client/caller), but because of the throws clause in your FunctionalInterface method, I always have to write the try-catch.
What do I do if I really know this API throws IOException
Then probably we are confusing FunctionalInterface with typical Interfaces. If you know this API will throw IOException, then most probably you also know some default/abstract behavior as well. I think you should define an interface and deploy your library (with default/abstract implementation) as follows
public interface MyAmazingAPI {
Integer myMethod(String s) throws IOException;
}
But, the try-catch problem still exists for the client. If I use your API in stream, I still need to handle IOException in hideous try-catch block.
Provide a default stream-friendly API as follows
public interface MyAmazingAPI {
Integer myMethod(String s) throws IOException;
default Optional<Integer> myMethod(String s, Consumer<? super Exception> exceptionConsumer) {
try {
return Optional.ofNullable(this.myMethod(s));
} catch (Exception e) {
if (exceptionConsumer != null) {
exceptionConsumer.accept(e);
} else {
e.printStackTrace();
}
}
return Optional.empty();
}
}
The default method takes the consumer object as argument, which will be responsible to handle the exception. Now, from client's point of view, the code will look like this
strStream.map(str -> amazingAPIs.myMethod(str, Exception::printStackTrace))
.filter(Optional::isPresent)
.map(Optional::get).collect(toList());
Nice right? Of course, logger or other handling logic could be used instead of Exception::printStackTrace.
You can also expose a method similar to https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html#exceptionally-java.util.function.Function- . Meaning that you can expose another method, which will contain the exception from previous method call. The disadvantage is that you are now making your APIs stateful, which means that you need to handle thread-safety and which will be eventually become a performance hit. Just an option to consider though.
By default, Java 8 Function does not allow to throw exception and as suggested in multiple answers there are many ways to achieve it, one way is:
#FunctionalInterface
public interface FunctionWithException<T, R, E extends Exception> {
R apply(T t) throws E;
}
Define as:
private FunctionWithException<String, Integer, IOException> myMethod = (str) -> {
if ("abc".equals(str)) {
throw new IOException();
}
return 1;
};
And add throws or try/catch the same exception in caller method.
I use an overloaded utility function called unchecked() which handles multiple use-cases.
SOME EAMPLE USAGES
unchecked(() -> new File("hello.txt").createNewFile());
boolean fileWasCreated = unchecked(() -> new File("hello.txt").createNewFile());
myFiles.forEach(unchecked(file -> new File(file.path).createNewFile()));
SUPPORTING UTILITIES
public class UncheckedUtils {
#FunctionalInterface
public interface ThrowingConsumer<T> {
void accept(T t) throws Exception;
}
#FunctionalInterface
public interface ThrowingSupplier<T> {
T get() throws Exception;
}
#FunctionalInterface
public interface ThrowingRunnable {
void run() throws Exception;
}
public static <T> Consumer<T> unchecked(
ThrowingConsumer<T> throwingConsumer
) {
return i -> {
try {
throwingConsumer.accept(i);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
};
}
public static <T> T unchecked(
ThrowingSupplier<T> throwingSupplier
) {
try {
return throwingSupplier.get();
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
public static void unchecked(
ThrowingRunnable throwing
) {
try {
throwing.run();
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
}
You can use ET for this. ET is a small Java 8 library for exception conversion/translation.
With ET it looks like this:
// Do this once
ExceptionTranslator et = ET.newConfiguration().done();
...
// if your method returns something
Function<String, Integer> f = (t) -> et.withReturningTranslation(() -> myMethod(t));
// if your method returns nothing
Consumer<String> c = (t) -> et.withTranslation(() -> myMethod(t));
ExceptionTranslator instances are thread safe an can be shared by multiple components. You can configure more specific exception conversion rules (e.g. FooCheckedException -> BarRuntimeException) if you like.
If no other rules are available, checked exceptions are automatically converted to RuntimeException.
(Disclaimer: I am the author of ET)
If you don't mind using a third party library, with cyclops-react, a library I contribute to, you can use the FluentFunctions API to write
Function<String, Integer> standardFn = FluentFunctions.ofChecked(this::myMethod);
ofChecked takes a jOOλ CheckedFunction and returns the reference softened back to a standard (unchecked) JDK java.util.function.Function.
Alternatively you can keep working with the captured function via the FluentFunctions api!
For example to execute your method, retrying it up to 5 times and logging it's status you can write
FluentFunctions.ofChecked(this::myMethod)
.log(s->log.debug(s),e->log.error(e,e.getMessage())
.try(5,1000)
.apply("my param");
What I'm doing is to allow the user to give the value he actually want in case of exception .
So I've something looking like this
public static <T, R> Function<? super T, ? extends R> defaultIfThrows(FunctionThatThrows<? super T, ? extends R> delegate, R defaultValue) {
return x -> {
try {
return delegate.apply(x);
} catch (Throwable throwable) {
return defaultValue;
}
};
}
#FunctionalInterface
public interface FunctionThatThrows<T, R> {
R apply(T t) throws Throwable;
}
And this can then be call like :
defaultIfThrows(child -> child.getID(), null)
Use Jool Library or say jOOλ library from JOOQ. It not only provides unchecked exception handled interfaces but also provides Seq class with lots of useful methods.
Also, it contains Functional Interfaces with up to 16 parameters. Also, it provides Tuple class which is used in different scenarios.
Jool Git Link
Specifically in library lookup for org.jooq.lambda.fi.util.function package. It contains all the Interfaces from Java-8 with Checked prepended. See below for reference:-
If you have lombok, you can annotate your method with #SneakyThrows
SneakyThrow does not silently swallow, wrap into RuntimeException, or otherwise modify any exceptions of the listed checked exception types. The JVM does not check for the consistency of the checked exception system; javac does, and this annotation lets you opt out of its mechanism.
https://projectlombok.org/features/SneakyThrows
Several of the offered solutions use a generic argument of E to pass in the type of the exception which gets thrown.
Take that one step further, and rather than passing in the type of the exception, pass in a Consumer of the type of exception, as in...
Consumer<E extends Exception>
You might create several re-usable variations of Consumer<Exception> which would cover the common exception handling needs of your application.
I will do something generic:
public interface Lambda {
#FunctionalInterface
public interface CheckedFunction<T> {
T get() throws Exception;
}
public static <T> T handle(CheckedFunction<T> supplier) {
try {
return supplier.get();
} catch (Exception exception) {
throw new RuntimeException(exception);
}
}
}
usage:
Lambda.handle(() -> method());
I'm the author of a tiny lib with some generic magic to throw any Java Exception anywhere without the need of catching them nor wrapping them into RuntimeException.
Usage:
unchecked(() -> methodThrowingCheckedException())
public class UncheckedExceptions {
/**
* throws {#code exception} as unchecked exception, without wrapping exception.
*
* #return will never return anything, return type is set to {#code exception} only to be able to write <code>throw unchecked(exception)</code>
* #throws T {#code exception} as unchecked exception
*/
#SuppressWarnings("unchecked")
public static <T extends Throwable> T unchecked(Exception exception) throws T {
throw (T) exception;
}
#FunctionalInterface
public interface UncheckedFunction<R> {
R call() throws Exception;
}
/**
* Executes given function,
* catches and rethrows checked exceptions as unchecked exceptions, without wrapping exception.
*
* #return result of function
* #see #unchecked(Exception)
*/
public static <R> R unchecked(UncheckedFunction<R> function) {
try {
return function.call();
} catch (Exception e) {
throw unchecked(e);
}
}
#FunctionalInterface
public interface UncheckedMethod {
void call() throws Exception;
}
/**
* Executes given method,
* catches and rethrows checked exceptions as unchecked exceptions, without wrapping exception.
*
* #see #unchecked(Exception)
*/
public static void unchecked(UncheckedMethod method) {
try {
method.call();
} catch (Exception e) {
throw unchecked(e);
}
}
}
source: https://github.com/qoomon/unchecked-exceptions-java
For me the preferred solution is to use Lombok. It is a nice library anyway.
Instead of:
Integer myMethod(String s) throws IOException
you will have
import lombok.SneakyThrows;
#SneakyThrows
Integer myMethod(String s)
The exception is still thrown but you do not need to declare it with throws.
public void frankTest() {
int pageId= -1;
List<Book> users= null;
try {
//Does Not Compile: Object page=DatabaseConnection.getSpringConnection().queryForObject("SELECT * FROM bookmark_page", (rw, n) -> new Portal(rw.getInt("id"), "", users.parallelStream().filter(uu -> uu.getVbid() == rw.getString("user_id")).findFirst().get(), rw.getString("name")));
//Compiles:
Object page= DatabaseConnection.getSpringConnection().queryForObject("SELECT * FROM bookmark_page", (rw, n) -> {
try {
final Book bk= users.stream().filter(bp -> {
String name= null;
try {
name = rw.getString("name");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bp.getTitle().equals(name);
}).limit(1).collect(Collectors.toList()).get(0);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new Portal(rw.getInt("id"), "", users.get(0), rw.getString("name"));
} );
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Related
Using Optional.map() instead of Optional.ifPresent() to execute code that may throw an exception and returning any exceptions as the return value
I'm trying to get a feel for the proper way to use the java.util.Optional class. Given the following example code: public void doSomething(File destDir, String classifier) throws IOException { URL resourceRoot = introspectionService.getResourceRoot(pattern); if (resourceRoot != null) { JarFile jarFile = (JarFile) resourceRoot.getContent(); //this can throw IOException ... snipped for brevity ... } } I would like to convert this method to use an Optional<URL> value from the introspectionService. public void doSomething(File destDir, String classifier) throws IOException { Optional<URL> resourceRoot = introspectionService.getResourceRoot(pattern); resourceRoot.ifPresent(rootUrl-> { JarFile jarFile = (JarFile) rootUrl.getContent(); //this can throw IOException ... snipped for brevity ... }); } This obviously fails to compile because the signature of ifPresent() takes a Consumer which does not allow exceptions to be thrown from the accept() method. I came up with the following work-around and was wondering if there is a better way: public void doSomething(File destDir, String classifier) throws IOException { Optional<URL> resourceRoot = introspectionService.getResourceRoot(pattern); Optional<IOException> ioException = resourceRoot.map(rootUrl-> { try { JarFile jarFile = (JarFile) rootUrl.getContent(); //this can throw IOException ... snipped for brevity ... return null; } catch(IOException e) { return e; } }); if (ioException.isPresent()) { throw ioException.get(); } } Is there a better alternative?
EDIT: As MC Emperor suggested, you can catch the IOException and then throw an UncheckedIOException. Here's a more generic way to do it by making the compiler think a checked exception is really unchecked. resourceRoot.map(rootUrl-> { try { JarFile jarFile = (JarFile) rootUrl.getContent(); //this can throw IOException ... snipped for brevity ... return null; } catch(IOException e) { sneakyThrow(e); } }); You can define sneakyThrow as given below (I got it from this article). Because of type erasure and type inference (T is inferred to either RuntimeException or Error), it compiles without errors but still runs as if you actually said throw e. public static <T extends Throwable> void sneakyThrow(Throwable t) throws T { throw (T) t; } Lombok has a SneakyThrows annotation that helps you do this. This way, you don't have to wrap it in a RuntimeException or anything - the exception is simply an IOException at runtime too.
I think this is not the right place to use Optional while you have other classes that handle the same thing you looking for. it's worth to look at the Files class and change your design accordingly
Better way to continue after exceptions in java
Assuming I have to read from a file, and then construct a java object out of it. PersonData p = new PersonData(); p.setName(readTokenAsString()); p.setAge(AgeConverter.createFromDateOfBirth(readTokenAsString())); // this throws a checked exception if the date of birth is mal-formed. //... a list of methods that throws exception as AgeConverter Behavior I want: If one attribute has problem, just ignore it and keep process other attributes. Solution I can think of: try { p.setAge1(...); } catch (Exception e) { //log and ignore } try { p.setAge2(...); } catch (Exception e) { //log and ignore } //repeat for each attribute Question: Is there better way to do this to avoid repetition? Functional style maybe? a) What's the best approach if I cannot modify PersonData class. b) What's the best approach if I can rewrite PersonData class.
Given your current declaration, I would do it as follows. Define a #FunctionalInterface to which you can pass your I/O logic: #FunctionalInterface public interface CheckedSupplier<T> { T getValue() throws Exception; } Define an utility method that consumes the #FunctionaInterface: public static final <T> T getValueWithDefault(CheckedSupplier<T> supplier, T defaultValue) { try { return supplier.getValue(); } catch (Exception e){ return defaultValue; } } Use the utility method as follows: PersonData p = new PersonData(); p.setName(getValueWithDefault(() -> readTokenAsString(), "default")); p.setAge(getValueWithDefault(() -> AgeConverter.createFromDateOfBirth(readTokenAsString()), 0)); This should do the trick regardless of weather you want modify the PersonData class or not.
If you use Java 8 you can do something like this. Create your own functional interface with one method that throws Exception public interface MyConsumer<T> { public void process(T t) throws Exception; } And create a static method to use that interface public static <T> void setAndLogException(T value, MyConsumer<T> consumer) { try { consumer.process(value); } catch (Exception e) { // log exception } } And then using it like setAndLogException(AgeConverter.createFromDateOfBirth(readTokenAsString()), p::setAge);
You can also use solution provided by this: https://stackoverflow.com/a/28659553/6648303 This solution won't complain at compile phase about checked Exceptions. It would be something like this: public static void ignoringExc(RunnableExc r) { try { r.run(); } catch (Exception e) { } } #FunctionalInterface public interface RunnableExc { void run() throws Exception; } and then: PersonData p = new PersonData(); ignoringExc(() -> p.setName(readTokenAsString())); ...
How to avoid repeating complex exception handling code in a wrapper class?
I have this class that wraps an object: public class MyWrapper implements MyInterface { private MyInterface wrappedObj; public MyWrapper(MyInterface obj) { this.wrappedObj = obj; } #Override public String ping(String s) { return wrappedObj.ping(s); } #Override public String doSomething(int i, String s) { return wrappedObj.doSomething(i, s); } // many more methods ... } Now I want to add complex exception handling around the wrappedObj call. It is the same for all the methods. How do I avoid repeating the same exception handling code over and over?
If your exception handling is fully generic you could implement the wrapper as InvocationHandler: public class ExceptionHandler implements java.lang.reflect.InvocationHandler { public ExceptionHandler(Object impl) { impl_ = impl; } #Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { try { return method.invoke(impl_, args); } catch (Exception e) { // do exception handling magic and return something useful return ...; } } private Object impl_; } and then wrap it around an instance as follows: MyInterface instance = ... MyInterface wrapper = (MyInterface)java.lang.reflect.Proxy.newProxyInstance( instance.getClass().getClassLoader(), new Class[] { MyInterface.class }, new ExceptionHandler(instance)); wrapper.ping("hello");
If you want to avoid the cost of reflection, than just use a router function. #Override public String ping(String s) { return (String) call("ping"); } private Object call(String func) { try { switch(func) { case "ping": return wrappedObj.ping(s); // ... rest of functions ... // } } catch(Exception e) { log(e); } } The compiler can than effectively just jump to the function without pulling up Object specs or handlers. (A smart enough compiler may even just compile this to identical execution code as your current code, especially if you can cut the cast by always returning the same kind of object) If you don't care about the thread and just want a default exception handler... For the whole Java Runtime, call Thread.setDefaultUncaughtExceptionHandler For a ThreadGroup, override ThreadGroup.uncaughtException For a single Thread, call Thread.setUncaughtExceptionHandler The advantage to a default handler, is that you can then add specific error handlers where needed, but the down side is you do lose the executing thread on error.
How to use throws keyword in lambda expressions java 8? [duplicate]
I know how to create a reference to a method that has a String parameter and returns an int, it's: Function<String, Integer> However, this doesn't work if the function throws an exception, say it's defined as: Integer myMethod(String s) throws IOException How would I define this reference?
You'll need to do one of the following. If it's your code, then define your own functional interface that declares the checked exception: #FunctionalInterface public interface CheckedFunction<T, R> { R apply(T t) throws IOException; } and use it: void foo (CheckedFunction f) { ... } Otherwise, wrap Integer myMethod(String s) in a method that doesn't declare a checked exception: public Integer myWrappedMethod(String s) { try { return myMethod(s); } catch(IOException e) { throw new UncheckedIOException(e); } } and then: Function<String, Integer> f = (String t) -> myWrappedMethod(t); or: Function<String, Integer> f = (String t) -> { try { return myMethod(t); } catch(IOException e) { throw new UncheckedIOException(e); } };
You can actually extend Consumer (and Function etc.) with a new interface that handles exceptions -- using Java 8's default methods! Consider this interface (extends Consumer): #FunctionalInterface public interface ThrowingConsumer<T> extends Consumer<T> { #Override default void accept(final T elem) { try { acceptThrows(elem); } catch (final Exception e) { // Implement your own exception handling logic here.. // For example: System.out.println("handling an exception..."); // Or ... throw new RuntimeException(e); } } void acceptThrows(T elem) throws Exception; } Then, for example, if you have a list: final List<String> list = Arrays.asList("A", "B", "C"); If you want to consume it (eg. with forEach) with some code that throws exceptions, you would traditionally have set up a try/catch block: final Consumer<String> consumer = aps -> { try { // maybe some other code here... throw new Exception("asdas"); } catch (final Exception ex) { System.out.println("handling an exception..."); } }; list.forEach(consumer); But with this new interface, you can instantiate it with a lambda expression and the compiler will not complain: final ThrowingConsumer<String> throwingConsumer = aps -> { // maybe some other code here... throw new Exception("asdas"); }; list.forEach(throwingConsumer); Or even just cast it to be more succinct!: list.forEach((ThrowingConsumer<String>) aps -> { // maybe some other code here... throw new Exception("asda"); }); Update Looks like there's a very nice utility library part of Durian called Errors which can be used to solve this problem with a lot more flexibility. For example, in my implementation above I've explicitly defined the error handling policy (System.out... or throw RuntimeException), whereas Durian's Errors allow you to apply a policy on the fly via a large suite of utility methods. Thanks for sharing it, #NedTwigg!. Sample usage: list.forEach(Errors.rethrow().wrap(c -> somethingThatThrows(c)));
I think Durian's Errors class combines many of the pros of the various suggestions above. Wrap a throwing function to a standard Java 8 functional interface. Easily specify various policies for handling errors When wrapping a method that returns a value, there is an important distinction between specifying a default value or rethrowing a RuntimeException. Throwing versions of Java 8's functional interfaces Similar to fge's answer Standard interfaces for throwing specific exceptions Which addresses Zoltán's concern To include Durian in your project, you can either: grab it from jcenter or maven central at com.diffplug.durian:durian:3.3.0 or just copy paste just two small classes into your code: Throwing.java and Errors.java
This is not specific to Java 8. You are trying to compile something equivalent to: interface I { void m(); } class C implements I { public void m() throws Exception {} //can't compile }
Disclaimer: I haven't used Java 8 yet, only read about it. Function<String, Integer> doesn't throw IOException, so you can't put any code in it that throws IOException. If you're calling a method that expects a Function<String, Integer>, then the lambda that you pass to that method can't throw IOException, period. You can either write a lambda like this (I think this is the lambda syntax, not sure): (String s) -> { try { return myMethod(s); } catch (IOException ex) { throw new RuntimeException(ex); // (Or do something else with it...) } } Or, if the method you're passing the lambda to is one you wrote yourself, you can define a new functional interface and use that as the parameter type instead of Function<String, Integer>: public interface FunctionThatThrowsIOException<I, O> { O apply(I input) throws IOException; }
If you don't mind to use a 3rd party lib (Vavr) you could write CheckedFunction1<String, Integer> f = this::myMethod; It also has the so-called Try monad which handles errors: Try(() -> f.apply("test")) // results in a Success(Integer) or Failure(Throwable) .map(i -> ...) // only executed on Success ... Please read more here. Disclaimer: I'm the creator of Vavr.
Sneaky throw idiom enables bypassing CheckedException of Lambda expression. Wrapping a CheckedException in a RuntimeException is not good for strict error handling. It can be used as a Consumer function used in a Java collection. Here is a simple and improved version of jib's answer. import static Throwing.rethrow; #Test public void testRethrow() { thrown.expect(IOException.class); thrown.expectMessage("i=3"); Arrays.asList(1, 2, 3).forEach(rethrow(e -> { int i = e.intValue(); if (i == 3) { throw new IOException("i=" + i); } })); } This just wrapps the lambda in a rethrow. It makes CheckedException rethrow any Exception that was thrown in your lambda. public final class Throwing { private Throwing() {} #Nonnull public static <T> Consumer<T> rethrow(#Nonnull final ThrowingConsumer<T> consumer) { return consumer; } /** * The compiler sees the signature with the throws T inferred to a RuntimeException type, so it * allows the unchecked exception to propagate. * * http://www.baeldung.com/java-sneaky-throws */ #SuppressWarnings("unchecked") #Nonnull public static <E extends Throwable> void sneakyThrow(#Nonnull Throwable ex) throws E { throw (E) ex; } } Find a complete code and unit tests here.
You could however create your own FunctionalInterface that throws as below.. #FunctionalInterface public interface UseInstance<T, X extends Throwable> { void accept(T instance) throws X; } then implement it using Lambdas or references as shown below. import java.io.FileWriter; import java.io.IOException; //lambda expressions and the execute around method (EAM) pattern to //manage resources public class FileWriterEAM { private final FileWriter writer; private FileWriterEAM(final String fileName) throws IOException { writer = new FileWriter(fileName); } private void close() throws IOException { System.out.println("close called automatically..."); writer.close(); } public void writeStuff(final String message) throws IOException { writer.write(message); } //... public static void use(final String fileName, final UseInstance<FileWriterEAM, IOException> block) throws IOException { final FileWriterEAM writerEAM = new FileWriterEAM(fileName); try { block.accept(writerEAM); } finally { writerEAM.close(); } } public static void main(final String[] args) throws IOException { FileWriterEAM.use("eam.txt", writerEAM -> writerEAM.writeStuff("sweet")); FileWriterEAM.use("eam2.txt", writerEAM -> { writerEAM.writeStuff("how"); writerEAM.writeStuff("sweet"); }); FileWriterEAM.use("eam3.txt", FileWriterEAM::writeIt); } void writeIt() throws IOException{ this.writeStuff("How "); this.writeStuff("sweet "); this.writeStuff("it is"); } }
You can use unthrow wrapper Function<String, Integer> func1 = s -> Unthrow.wrap(() -> myMethod(s)); or Function<String, Integer> func2 = s1 -> Unthrow.wrap((s2) -> myMethod(s2), s1);
You can. Extending #marcg 's UtilException and adding generic <E extends Exception> where necessary: this way, the compiler will force you again to add throw clauses and everything's as if you could throw checked exceptions natively on java 8's streams. public final class LambdaExceptionUtil { #FunctionalInterface public interface Function_WithExceptions<T, R, E extends Exception> { R apply(T t) throws E; } /** * .map(rethrowFunction(name -> Class.forName(name))) or .map(rethrowFunction(Class::forName)) */ public static <T, R, E extends Exception> Function<T, R> rethrowFunction(Function_WithExceptions<T, R, E> function) throws E { return t -> { try { return function.apply(t); } catch (Exception exception) { throwActualException(exception); return null; } }; } #SuppressWarnings("unchecked") private static <E extends Exception> void throwActualException(Exception exception) throws E { throw (E) exception; } } public class LambdaExceptionUtilTest { #Test public void testFunction() throws MyTestException { List<Integer> sizes = Stream.of("ciao", "hello").<Integer>map(rethrowFunction(s -> transform(s))).collect(toList()); assertEquals(2, sizes.size()); assertEquals(4, sizes.get(0).intValue()); assertEquals(5, sizes.get(1).intValue()); } private Integer transform(String value) throws MyTestException { if(value==null) { throw new MyTestException(); } return value.length(); } private static class MyTestException extends Exception { } }
I had this problem with Class.forName and Class.newInstance inside a lambda, so I just did: public Object uncheckedNewInstanceForName (String name) { try { return Class.forName(name).newInstance(); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { throw new RuntimeException(e); } } Inside the lambda, instead of calling Class.forName("myClass").newInstance() I just called uncheckedNewInstanceForName ("myClass")
Create a custom return type that will propagate the checked exception. This is an alternative to creating a new interface that mirrors the existing functional interface with the slight modification of a "throws exception" on the functional interface's method. Definition CheckedValueSupplier public static interface CheckedValueSupplier<V> { public V get () throws Exception; } CheckedValue public class CheckedValue<V> { private final V v; private final Optional<Exception> opt; public Value (V v) { this.v = v; } public Value (Exception e) { this.opt = Optional.of(e); } public V get () throws Exception { if (opt.isPresent()) { throw opt.get(); } return v; } public Optional<Exception> getException () { return opt; } public static <T> CheckedValue<T> returns (T t) { return new CheckedValue<T>(t); } public static <T> CheckedValue<T> rethrows (Exception e) { return new CheckedValue<T>(e); } public static <V> CheckedValue<V> from (CheckedValueSupplier<V> sup) { try { return CheckedValue.returns(sup.get()); } catch (Exception e) { return Result.rethrows(e); } } public static <V> CheckedValue<V> escalates (CheckedValueSupplier<V> sup) { try { return CheckedValue.returns(sup.get()); } catch (Exception e) { throw new RuntimeException(e); } } } Usage // Don't use this pattern with FileReader, it's meant to be an // example. FileReader is a Closeable resource and as such should // be managed in a try-with-resources block or in another safe // manner that will make sure it is closed properly. // This will not compile as the FileReader constructor throws // an IOException. Function<String, FileReader> sToFr = (fn) -> new FileReader(Paths.get(fn).toFile()); // Alternative, this will compile. Function<String, CheckedValue<FileReader>> sToFr = (fn) -> { return CheckedValue.from ( () -> new FileReader(Paths.get("/home/" + f).toFile())); }; // Single record usage // The call to get() will propagate the checked exception if it exists. FileReader readMe = pToFr.apply("/home/README").get(); // List of records usage List<String> paths = ...; //a list of paths to files Collection<CheckedValue<FileReader>> frs = paths.stream().map(pToFr).collect(Collectors.toList()); // Find out if creation of a file reader failed. boolean anyErrors = frs.stream() .filter(f -> f.getException().isPresent()) .findAny().isPresent(); What's going on? A single functional interface that throws a checked exception is created (CheckedValueSupplier). This will be the only functional interface which allows checked exceptions. All other functional interfaces will leverage the CheckedValueSupplier to wrap any code that throws a checked exception. The CheckedValue class will hold the result of executing any logic that throws a checked exception. This prevents propagation of a checked exception until the point at which code attempts to access the value that an instance of CheckedValue contains. The problems with this approach. We are now throwing "Exception" effectively hiding the specific type originally thrown. We are unaware that an exception occurred until CheckedValue#get() is called. Consumer et al Some functional interfaces (Consumer for example) must be handled in a different manner as they don't provide a return value. Function in lieu of Consumer One approach is to use a function instead of a consumer, which applies when handling streams. List<String> lst = Lists.newArrayList(); // won't compile lst.stream().forEach(e -> throwyMethod(e)); // compiles lst.stream() .map(e -> CheckedValueSupplier.from( () -> {throwyMethod(e); return e;})) .filter(v -> v.getException().isPresent()); //this example may not actually run due to lazy stream behavior Escalate Alternatively, you can always escalate to a RuntimeException. There are other answers that cover escalation of a checked exception from within a Consumer. Don't consume. Just avoid functional interfaces all together and use a good-ole-fashioned for loop.
Another solution using a Function wrapper would be to return either an instance of a wrapper of your result, say Success, if everything went well, either an instance of, say Failure. Some code to clarify things : public interface ThrowableFunction<A, B> { B apply(A a) throws Exception; } public abstract class Try<A> { public static boolean isSuccess(Try tryy) { return tryy instanceof Success; } public static <A, B> Function<A, Try<B>> tryOf(ThrowableFunction<A, B> function) { return a -> { try { B result = function.apply(a); return new Success<B>(result); } catch (Exception e) { return new Failure<>(e); } }; } public abstract boolean isSuccess(); public boolean isError() { return !isSuccess(); } public abstract A getResult(); public abstract Exception getError(); } public class Success<A> extends Try<A> { private final A result; public Success(A result) { this.result = result; } #Override public boolean isSuccess() { return true; } #Override public A getResult() { return result; } #Override public Exception getError() { return new UnsupportedOperationException(); } #Override public boolean equals(Object that) { if(!(that instanceof Success)) { return false; } return Objects.equal(result, ((Success) that).getResult()); } } public class Failure<A> extends Try<A> { private final Exception exception; public Failure(Exception exception) { this.exception = exception; } #Override public boolean isSuccess() { return false; } #Override public A getResult() { throw new UnsupportedOperationException(); } #Override public Exception getError() { return exception; } } A simple use case : List<Try<Integer>> result = Lists.newArrayList(1, 2, 3).stream(). map(Try.<Integer, Integer>tryOf(i -> someMethodThrowingAnException(i))). collect(Collectors.toList());
This problem has been bothering me as well; this is why I have created this project. With it you can do: final ThrowingFunction<String, Integer> f = yourMethodReferenceHere; There are a totla of 39 interfaces defined by the JDK which have such a Throwing equivalent; those are all #FunctionalInterfaces used in streams (the base Stream but also IntStream, LongStream and DoubleStream). And as each of them extend their non throwing counterpart, you can directly use them in lambdas as well: myStringStream.map(f) // <-- works The default behavior is that when your throwing lambda throws a checked exception, a ThrownByLambdaException is thrown with the checked exception as the cause. You can therefore capture that and get the cause. Other features are available as well.
There are a lot of great responses already posted here. Just attempting to solve the problem with a different perspective. Its just my 2 cents, please correct me if I am wrong somewhere. Throws clause in FunctionalInterface is not a good idea I think this is probably not a good idea to enforce throws IOException because of following reasons This looks to me like an anti-pattern to Stream/Lambda. The whole idea is that the caller will decide what code to provide and how to handle the exception. In many scenarios, the IOException might not be applicable for the client. For example, if the client is getting value from cache/memory instead of performing actual I/O. Also, the exceptions handling in streams becomes really hideous. For example, here is my code will look like if I use your API acceptMyMethod(s -> { try { Integer i = doSomeOperation(s); return i; } catch (IOException e) { // try catch block because of throws clause // in functional method, even though doSomeOperation // might not be throwing any exception at all. e.printStackTrace(); } return null; }); Ugly isn't it? Moreover, as I mentioned in my first point, that the doSomeOperation method may or may not be throwing IOException (depending on the implementation of the client/caller), but because of the throws clause in your FunctionalInterface method, I always have to write the try-catch. What do I do if I really know this API throws IOException Then probably we are confusing FunctionalInterface with typical Interfaces. If you know this API will throw IOException, then most probably you also know some default/abstract behavior as well. I think you should define an interface and deploy your library (with default/abstract implementation) as follows public interface MyAmazingAPI { Integer myMethod(String s) throws IOException; } But, the try-catch problem still exists for the client. If I use your API in stream, I still need to handle IOException in hideous try-catch block. Provide a default stream-friendly API as follows public interface MyAmazingAPI { Integer myMethod(String s) throws IOException; default Optional<Integer> myMethod(String s, Consumer<? super Exception> exceptionConsumer) { try { return Optional.ofNullable(this.myMethod(s)); } catch (Exception e) { if (exceptionConsumer != null) { exceptionConsumer.accept(e); } else { e.printStackTrace(); } } return Optional.empty(); } } The default method takes the consumer object as argument, which will be responsible to handle the exception. Now, from client's point of view, the code will look like this strStream.map(str -> amazingAPIs.myMethod(str, Exception::printStackTrace)) .filter(Optional::isPresent) .map(Optional::get).collect(toList()); Nice right? Of course, logger or other handling logic could be used instead of Exception::printStackTrace. You can also expose a method similar to https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html#exceptionally-java.util.function.Function- . Meaning that you can expose another method, which will contain the exception from previous method call. The disadvantage is that you are now making your APIs stateful, which means that you need to handle thread-safety and which will be eventually become a performance hit. Just an option to consider though.
By default, Java 8 Function does not allow to throw exception and as suggested in multiple answers there are many ways to achieve it, one way is: #FunctionalInterface public interface FunctionWithException<T, R, E extends Exception> { R apply(T t) throws E; } Define as: private FunctionWithException<String, Integer, IOException> myMethod = (str) -> { if ("abc".equals(str)) { throw new IOException(); } return 1; }; And add throws or try/catch the same exception in caller method.
I use an overloaded utility function called unchecked() which handles multiple use-cases. SOME EAMPLE USAGES unchecked(() -> new File("hello.txt").createNewFile()); boolean fileWasCreated = unchecked(() -> new File("hello.txt").createNewFile()); myFiles.forEach(unchecked(file -> new File(file.path).createNewFile())); SUPPORTING UTILITIES public class UncheckedUtils { #FunctionalInterface public interface ThrowingConsumer<T> { void accept(T t) throws Exception; } #FunctionalInterface public interface ThrowingSupplier<T> { T get() throws Exception; } #FunctionalInterface public interface ThrowingRunnable { void run() throws Exception; } public static <T> Consumer<T> unchecked( ThrowingConsumer<T> throwingConsumer ) { return i -> { try { throwingConsumer.accept(i); } catch (Exception ex) { throw new RuntimeException(ex); } }; } public static <T> T unchecked( ThrowingSupplier<T> throwingSupplier ) { try { return throwingSupplier.get(); } catch (Exception ex) { throw new RuntimeException(ex); } } public static void unchecked( ThrowingRunnable throwing ) { try { throwing.run(); } catch (Exception ex) { throw new RuntimeException(ex); } } }
You can use ET for this. ET is a small Java 8 library for exception conversion/translation. With ET it looks like this: // Do this once ExceptionTranslator et = ET.newConfiguration().done(); ... // if your method returns something Function<String, Integer> f = (t) -> et.withReturningTranslation(() -> myMethod(t)); // if your method returns nothing Consumer<String> c = (t) -> et.withTranslation(() -> myMethod(t)); ExceptionTranslator instances are thread safe an can be shared by multiple components. You can configure more specific exception conversion rules (e.g. FooCheckedException -> BarRuntimeException) if you like. If no other rules are available, checked exceptions are automatically converted to RuntimeException. (Disclaimer: I am the author of ET)
If you don't mind using a third party library, with cyclops-react, a library I contribute to, you can use the FluentFunctions API to write Function<String, Integer> standardFn = FluentFunctions.ofChecked(this::myMethod); ofChecked takes a jOOλ CheckedFunction and returns the reference softened back to a standard (unchecked) JDK java.util.function.Function. Alternatively you can keep working with the captured function via the FluentFunctions api! For example to execute your method, retrying it up to 5 times and logging it's status you can write FluentFunctions.ofChecked(this::myMethod) .log(s->log.debug(s),e->log.error(e,e.getMessage()) .try(5,1000) .apply("my param");
What I'm doing is to allow the user to give the value he actually want in case of exception . So I've something looking like this public static <T, R> Function<? super T, ? extends R> defaultIfThrows(FunctionThatThrows<? super T, ? extends R> delegate, R defaultValue) { return x -> { try { return delegate.apply(x); } catch (Throwable throwable) { return defaultValue; } }; } #FunctionalInterface public interface FunctionThatThrows<T, R> { R apply(T t) throws Throwable; } And this can then be call like : defaultIfThrows(child -> child.getID(), null)
Use Jool Library or say jOOλ library from JOOQ. It not only provides unchecked exception handled interfaces but also provides Seq class with lots of useful methods. Also, it contains Functional Interfaces with up to 16 parameters. Also, it provides Tuple class which is used in different scenarios. Jool Git Link Specifically in library lookup for org.jooq.lambda.fi.util.function package. It contains all the Interfaces from Java-8 with Checked prepended. See below for reference:-
If you have lombok, you can annotate your method with #SneakyThrows SneakyThrow does not silently swallow, wrap into RuntimeException, or otherwise modify any exceptions of the listed checked exception types. The JVM does not check for the consistency of the checked exception system; javac does, and this annotation lets you opt out of its mechanism. https://projectlombok.org/features/SneakyThrows
Several of the offered solutions use a generic argument of E to pass in the type of the exception which gets thrown. Take that one step further, and rather than passing in the type of the exception, pass in a Consumer of the type of exception, as in... Consumer<E extends Exception> You might create several re-usable variations of Consumer<Exception> which would cover the common exception handling needs of your application.
I will do something generic: public interface Lambda { #FunctionalInterface public interface CheckedFunction<T> { T get() throws Exception; } public static <T> T handle(CheckedFunction<T> supplier) { try { return supplier.get(); } catch (Exception exception) { throw new RuntimeException(exception); } } } usage: Lambda.handle(() -> method());
I'm the author of a tiny lib with some generic magic to throw any Java Exception anywhere without the need of catching them nor wrapping them into RuntimeException. Usage: unchecked(() -> methodThrowingCheckedException()) public class UncheckedExceptions { /** * throws {#code exception} as unchecked exception, without wrapping exception. * * #return will never return anything, return type is set to {#code exception} only to be able to write <code>throw unchecked(exception)</code> * #throws T {#code exception} as unchecked exception */ #SuppressWarnings("unchecked") public static <T extends Throwable> T unchecked(Exception exception) throws T { throw (T) exception; } #FunctionalInterface public interface UncheckedFunction<R> { R call() throws Exception; } /** * Executes given function, * catches and rethrows checked exceptions as unchecked exceptions, without wrapping exception. * * #return result of function * #see #unchecked(Exception) */ public static <R> R unchecked(UncheckedFunction<R> function) { try { return function.call(); } catch (Exception e) { throw unchecked(e); } } #FunctionalInterface public interface UncheckedMethod { void call() throws Exception; } /** * Executes given method, * catches and rethrows checked exceptions as unchecked exceptions, without wrapping exception. * * #see #unchecked(Exception) */ public static void unchecked(UncheckedMethod method) { try { method.call(); } catch (Exception e) { throw unchecked(e); } } } source: https://github.com/qoomon/unchecked-exceptions-java
For me the preferred solution is to use Lombok. It is a nice library anyway. Instead of: Integer myMethod(String s) throws IOException you will have import lombok.SneakyThrows; #SneakyThrows Integer myMethod(String s) The exception is still thrown but you do not need to declare it with throws.
public void frankTest() { int pageId= -1; List<Book> users= null; try { //Does Not Compile: Object page=DatabaseConnection.getSpringConnection().queryForObject("SELECT * FROM bookmark_page", (rw, n) -> new Portal(rw.getInt("id"), "", users.parallelStream().filter(uu -> uu.getVbid() == rw.getString("user_id")).findFirst().get(), rw.getString("name"))); //Compiles: Object page= DatabaseConnection.getSpringConnection().queryForObject("SELECT * FROM bookmark_page", (rw, n) -> { try { final Book bk= users.stream().filter(bp -> { String name= null; try { name = rw.getString("name"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return bp.getTitle().equals(name); }).limit(1).collect(Collectors.toList()).get(0); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return new Portal(rw.getInt("id"), "", users.get(0), rw.getString("name")); } ); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
Java Exception Handling via Strategy Pattern?
I cooked up a class ExceptionHandler<T extends Exception, OptionalReturnType> (see below) to eliminate some (what I view as) boilerplate code which was cluttering up actual implementation, while still providing a hook for explicit Exception handling if desired in the future. For the most part, in my application (essential a scientific computation), there is no such thing as recovery from exceptions - I need a log of the problem so I can fix it, but otherwise I'm just going to re-run once the problem is corrected. Do other people do this (at least, in my specific application situation)? Is it dumb to do so (if yes, some explanation as to why would be nice)? ExceptionHandler: public abstract class ExceptionHandler<ExceptionType extends Exception,OptionalReturn> { public abstract OptionalReturn handle(ExceptionType e); //assorted boilerplate ExceptionHandling, e.g.: public static <ET extends Exception> ExceptionHandler<ET, ?> swallower(final boolean printStackTrace, final String string) { return new ExceptionHandler<ET,Object>() { #Override public Object handle(ET e) { if(printStackTrace) { e.printStackTrace(); } if(string!=null && !string.isEmpty()) { System.err.println(string); } return null; } }; } public static <ET extends Exception> ExceptionHandler<ET, ?> swallower() { return swallower(false,null); } } example use (which I'm in the process of chopping down so I'm actually not writing quite so much): public class Getter<From> implements Function<Future<? extends From>, From> { private ExceptionHandler<InterruptedException,?> IEH; private ExceptionHandler<ExecutionException,?> EEH; public static final ExceptionHandler<InterruptedException,?> IEH_SWALLOWER = ExceptionHandler.swallower(true,"Returning null."); public static final ExceptionHandler<ExecutionException,?> EEH_SWALLOWER = ExceptionHandler.swallower(true,"Returning null."); private Getter() { this(IEH_SWALLOWER,EEH_SWALLOWER); } private Getter(ExceptionHandler<InterruptedException,?> IEH, ExceptionHandler<ExecutionException,?> EEH) { this.IEH = IEH; this.EEH = EEH; } public static <T> Getter<T> make() { return new Getter<T>(); } public static <T> Getter<T> make(ExceptionHandler<InterruptedException,?> IEH, ExceptionHandler<ExecutionException,?> EEH) { return new Getter<T>(IEH, EEH); } #Override public From apply(Future<? extends From> from) { if (from==null) throw new NullPointerException("Null argument in call with Getter."); return getter(from, IEH, EEH); } private static <T> T getter(Future<T> src, ExceptionHandler<InterruptedException,?> IEH, ExceptionHandler<ExecutionException,?> EEH) { try { return src.get(); } catch (InterruptedException e) { IEH.handle(e); } catch (ExecutionException e) { EEH.handle(e); } return null; } } which is used with the Guava libraries to do some embarrassingly-parallel calculations, and makes the actual Iterable transformation of Futures into something like Iterables.transform(futureCollection,Getter.make()) instead of tangle of inner-classes and exception handling.
I find the code honestly hard to follow and understand. It's full of static which is usually a bad sign in OO design and it's hard to follow with the generics. Wouldn't something simpler like this work as well? private static <T> T getter(Future<T> src) { try { return src.get(); } catch (InterruptedException e) { handle( "some text"); } catch (ExecutionException e) { handle( e ) } return null; } You can implement as many handle method as necessary in a base class (or in a static utility class) and use them in the catch block as necessary. Methods will be selected based on the signature, so if you want to print the text, you pass the string, if you want the stack trace you pass the exception (or both). Which leads to the combinations: handle( String msg ) handle( Exception e ) handle( Exception e, String msg ) This solution has less if, which is usually a good sign as well. But I have maybe missed a point, given that the code you published is just an excerpt of the whole code. Have a look otherwise at this question, which is also related: Pluggable Error Handling Strategy EDIT If the solution I proposed above is too simple for your need, here are two other ways: public class AbstractGetter<From> implements Function<Future<? extends From>, From> { private abstract handleInterrupt( Exception e ); private abstract handleExecution( Exception e ); private static <T> T getter(Future<T> src ) { try { return src.get(); } catch (InterruptedException e) { handleInterrupt(e) } catch (ExecutionException e) { handleExecution(e) } return null; } } And you implement the X concrete class that correspond the various exception handling strategies. That's essentially the template pattern. You can still use delegation, but at a more coarse-grained level. Instead of providing individual handler, you provide a handler strategy. That's kind of variation of the strategy pattern then. public interface ErrorStrategy { public void handleInterrupt(Exception e); public void handleExecution(Exception e); } public class Getter<From> implements Function<Future<? extends From>, From> { ErrorStrategy handler = new DefaultErrorStrategy(). // default one public Getter<From>() { } public Getter<From>( ErrorStrategy h ) { this.handler = h. } private static <T> T getter(Future<T> src ) { try { return src.get(); } catch (InterruptedException e) { handler.handleInterrupt(e) } catch (ExecutionException e) { handler.handleExecution(e) } return null; } } You can create the X error handling strategies that you need.
I think it's a good solution, but it could benefit from an ExceptionHandlerFactory and some xml files.