title: How to catch IOException in javafx
body: <p>I'm trying to write data into a text file by pressing a button in JavaFx. The only problem, however is when I try to use the statement, "throws IOException" in my button processing method, things don't seem to work. Here is my code.</p>
  
  <pre><code>import javafx.application.Application;
  import javafx.event.ActionEvent;
  import javafx.stage.Stage;
  import javafx.scene.Scene;
  import javafx.scene.Group;
  import javafx.scene.control.Button;
  import java.io.*;
  import java.io.File;
  import java.io.FileInputStream;
  
  public class testingFx extends Application{
  //Create controls
  private Button write;
  private Scene main;
  private Button Exit;
  private Scene sceneMain; 
  private File records;
  private FileWriter fw;
  
  public static void main(String[] args){
     launch(args);
   }
  
  @Override
  
  public void start(Stage stage) throws IOException{
  
    //Create new file
    records = new File("records.txt");
    records.createNewFile();
    //Create FileWriter
    fw = new FileWriter(records);
  
    //Create root, format controls, scene, etc...
    Group root = new Group();
    write = new Button();
    write.setText("Write");
    write.setOnAction(this::processButtonPress);
    root.getChildren().addAll(write);
    main = new Scene(root,300,300);
    stage.setScene(main);
    stage.show();
  }
     public void processButtonPress(ActionEvent event) throws IOException{
     if (event.getSource() == write){
          //On button press write to file
          fw.write("Testing file writing");
          //Close filewriter
          fw.close();
       }
    }
  }
  </code></pre>
  
  <p>I've tried to find the answer online but the way I was taught to handle a button press differs from most others examples (the section that says (this::processButtonPress)). I'm not sure if using a try/catch statement would help me here as I don't any experience with those, forgive me. The error I specifically am getting is "error: incompatible thrown types IOException in method reference". Thank you for your help.</p>
  
  <p>I've tried to make this question on topic and easy to address. Please let me know if there are any glaring issues.</p>