Skip to main content
Post Closed as "Duplicate" by xenteros, Lucky, Manuel Allenspach, andreas, dimo414 java
Source Link
J. Carrer
  • 135
  • 1
  • 8

Handling exception with lambda expression

I have problem with lambda expression. My code:

public static void main(String[] args) { Function<String, String> lambda = path -> { String result = null; try { BufferedReader br = new BufferedReader(new FileReader(path)); String line; while ((line = br.readLine()) != null) { result = result + line; } } catch (IOException e) { e.printStackTrace(); } return result; }; } 

I'm trying now to make code like that:

public static void main(String[] args) throws IOException { Function<String, String> lambda = path -> { String result = null; BufferedReader br = new BufferedReader(new FileReader(path)); String line; while ((line = br.readLine()) != null) { result = result + line; } return result; }; } 

Is that possible? I can use only java.util.function. I try to delete try catch from "lambda" and my "main" method should be catching that Exception.