title: Java Lambda Exception Handling
body: <p>I have a problem with the handling of exceptions, without the use of try and catch, in lambdas.</p>
  
  <p>I found example code for which I need to make my own <code>Consumer</code> interface, but it doesn't work and I don't know how to fix it.</p>
  
  <pre><code>import java.util.function.Consumer;
  
  @FunctionalInterface
  public interface ThrowingConsumer&lt;T, E extends Exception&gt; {
      void accept(T t) throws E;
  
      static &lt;T&gt; Consumer&lt;T&gt; throwingConsumerWrapper(
              ThrowingConsumer&lt;T, Exception&gt; throwingConsumer) {
  
          return i -&gt; {
              try {
                  throwingConsumer.accept(i);
              } catch (Exception ex) {
                  throw new RuntimeException(ex);
              }
          };
      }
  }
  </code></pre>
  
  <p>Here is the lambda in which I need to handle <code>IOException</code>. This exception must be thrown from main. I do not wish to use try and catch in <code>main</code>.</p>
  
  <pre><code>public static void main(String[] args) throws IOException {
      Function&lt;String, List&lt;String&gt;&gt; flines = fileName -&gt; {
          Scanner scanner;
          ArrayList&lt;String&gt; wiersze = new ArrayList&lt;&gt;();
          File f = new File(fileName);
  
          scanner = new Scanner(f); // here :(
  
          while (scanner.hasNext()) {
              wiersze.add(scanner.nextLine());
          }
  
          scanner.close();
  
          return wiersze;
      };
  }
  </code></pre>
  
  <p>I am new to Java, so please could you explain to me how to fix this problem in the easiest way.</p>