title: Unhandled IOException Inside Java List.stream.forEach Lambda Expression
body: <p>I am using the Stream API introduced in Java 8 to run a method for each string in a list.</p>
  
  <pre><code>public boolean initFile() throws IOException
  {
      if (this.outFile.exists()) {
          this.outFile.delete();
      }
      return this.outFile.createNewFile();
  }
  
  public void writeStringToFile(String str, boolean addNewLine) throws IOException
  {
      if (this.mode != FileMode.READ) {
          if (this.initFile()) {
              FileWriter fileWriter;
              if (this.mode == FileMode.APPEND)
                  fileWriter = new FileWriter(outFile.getAbsolutePath(), true);
              else
                  fileWriter = new FileWriter(outFile.getAbsolutePath());
              fileWriter.write(str);
              if (addNewLine)
                  fileWriter.write("\n");
              fileWriter.close();
          } else {
              throw new IOException("IO Error: The file could not be initialized.");
          }
      } else {
          throw new IOException("Cannot write to a file with the mode FileMode.READ");
      }
  }
  
  public void writeListToFile(List&lt;String&gt; strings, boolean addNewLine) throws IOException
  {
      strings.stream().forEach(s -&gt; this.writeStringToFile(s, addNewLine)); // Unhandled IOException from writeStringToFile
  }
  </code></pre>
  
  <p>As you can see, the method signature states to throw all IOExceptions and the writeToString method inside the stream has the potential to throw an IOException. However, the Java compiler is giving me an error stating that there is an unhandled IOException on the stream line.</p>
  
  <p>Why would the exception inside the stream not get thrown like any other exception inside the method? Is there any way that I can remedy this without putting a try-catch statement in the forEach lambda expression?</p>