I recently discovered that I wrote many blocks of the following form:
try {
return Optional.of(thing.doSomething(arg));
} catch (Exception e) {
System.out.println(e.getMessage());
return Optional.empty();
}
This is necessary because some methods signal that they can throw exceptions, and Eclipse yells at me if I do not surround these methods with try / catch blocks.
So I wrote this:
public static <A, T> Optional<T> tryOpt (Function<A, T> f, A arg) {
try {
return Optional.of(f.apply(arg));
} catch (Exception e) {
System.out.println(e.getMessage());
return Optional.empty();
}
}
So, any function I pass to tryOpt is enclosed in a try / catch block and executed safely, and its result is returned as optional. But Eclipse is still yelling at me for using it:
return tryOpt(
((x) -> thing.doSomething(x)),
arg);
, - Eclipse / java, , try/catch? - , java, - ? , , Eclipse ?