PaBo :
I have multiple objects with value stored in -
object.getList().get(0).getSomeObject.getName(0)
It's possible, that list is empty so-
object.getList().get(0)
throws a NPE
I want to pass-
object.getList().get(0).getSomeObject.getName()
to another method and handle the exception there.
For example:
// calling the method
myMethod(object.getList().get(0).getSomeObject.getName());
public void myMethod(Object o){
try {
String name = o;
}catch (Exception e){
// do something
}
}
Is it possible to do something like that -
EvaluateLaterObject elo = new EvaluateLaterObject(object.getList().get(0).getSomeObject.getName());
myMethod(elo);
public void myMethod(EvaluateLaterObject elo){
try {
String name = elo.getValue();
}catch (Exception e){
// do something
}
}
Thank you in advance!
Michael :
You can use a functional interface such as Supplier
and a lambda
myMethod(() -> object.getList().get(0).getSomeObject.getName());
public void myMethod(Supplier<String>){
try {
String name = elo.get();
}catch (Exception e){
// do something
}
}
If you want to support any exception, you'll need to define your own functional interface e.g.
@FunctionalInterface
interface ThrowableSupplier<T>
{
T get() throws Throwable;
}
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=326234&siteId=1