package codingtest;
public class Test2 {
static interface F {
int apply(int x) throws Exception;
}
public static int run(F f) {
try {
return f.apply(3);
} catch (Exception E){
return 7;
}
}
public static void main(String[] args) {
F f = (x) -> {
if(x>2) {
throw new Exception();
}
return x*2;
};
System.out.print(run(f) + run((int n) -> n+9));
}
}
Этот код возвращает 19. Я понимаю использование лямбда-функций и обработку исключений, но не понимаю, почему он возвращает это значение. Не могли бы вы объяснить?
static interface F { int apply(int x) throws Exception; }
public static int run(F f) { try { return f.apply(3); } catch (Exception E){ return 7; } }
public static void main(String[] args) { F f = (x) -> { if(x>2) { throw new Exception(); } return x*2; };
System.out.print(run(f) + run((int n) -> n+9)); } } [/code] Этот код возвращает 19. Я понимаю использование лямбда-функций и обработку исключений, но не понимаю, почему он возвращает это значение. Не могли бы вы объяснить?