title: throw Exception in lambda(or equivalent) - javafx
body: <p>I am trying to get a popup to come up asking the user if they wish to exit the program everytime they click the exit button (x in the the top left corner). However I am using FXML and have to throw an exception when I try to load the popup with <code>primaryStage.setOnCloseRequest(new EventHandler&lt;WindowEvent&gt;{}</code> I keep getting a unhandled Exception error even when I throw the error in the method I am calling from. I have tried try and catch and while there isn't an error the code doesn't run. I am currently trying to see if I can wrap the method in another so I can maybe get around the exception however nothing has seemed to work. Is there any way to use <code>setOnCloseRequest</code> with a thrown <code>Exception</code>?</p>
  
  <pre><code>public class Main extends Application  {
  
      @Override
      public void start(Stage primaryStage)throws Exception  {
          File f = new File("Goal.txt");
          boolean bool = false;
  
          if (f.exists() )
          {
              FXMLLoader loader = new FXMLLoader(getClass().getResource("MainPage.fxml"));
              Parent root = loader.load();
              primaryStage.setTitle("Money Saver Program");
              primaryStage.setScene(new Scene(root, 600, 400));
              primaryStage.show();
          }
          else
          {
              bool = f.createNewFile();
              Parent root = FXMLLoader.load(getClass().getResource("OpeningPage.fxml"));
              primaryStage.setTitle("Money Saver Program");
              primaryStage.setScene(new Scene(root, 638, 400));
              primaryStage.show();
          }
          primaryStage.setOnCloseRequest(new EventHandler&lt;WindowEvent&gt;() {
  
              public void handle(WindowEvent event) {
                  event.consume();
                  closeProgram();
              }
          });
  
      }
  
      public static void main(String[] args) throws Exception {
          launch(args);
      }
  
      public void closeProgram(){
          try{internalCloseProgram();}catch (Exception e){}
      }
  
      public void internalCloseProgram()throws Exception{
          FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("FinalExitMessage.fxml"));
          Parent root = fxmlLoader.load();
          Stage stage = new Stage();
          stage.setScene(new Scene(root));
          stage.setTitle("Money Saver Program");
          stage.show();
      }
  
  }
  </code></pre>