public controllerMethod() throws UnsupportedEncodingException {
getVehicles(req);
}
public List<vehicles> getVehicles(A req) throws UnsupportedEncodingException{
someObject.forEach(obj -> {
getVehicles2(req); //try catch resolves but why wouldn't throws handle this ?
}
}
public getVehicles2(A req) throws UnsupportedEncodingException{
}
我试图从getVehicles调用getVehicles2()。编译器抱怨这样做有一个未处理的异常。不会使用throws声明异常不足以将其传播到父控制器方法。 Try / catch会解决错误,但我认为声明抛出会将相关错误传播给调用方法。
答案 0 :(得分:1)
检查消费者的签名。如果将lambda表达式扩展为匿名类,则会得到:
new ArrayList<>().forEach(new Consumer<Object>() {
@Override
public void accept(Object obj) {
getVehicles2(req);
}
}
如您所见, foreach 收到 Consumer ,其 accept 方法没有您需要的“throws UnsupportedEncodingException”