0

Previously that was my code:

import java.io.FileNotFoundException;

public class Bundesliga extends Application {
    public static void main(String[] args) throws FileNotFoundException {
        PlayerManager league = new PlayerManager();
        league.uruchom();
    }
}

Of course there is another class PlayerManager but I think it isn't needed. Everything worked very well and there were no errors. Then I tried to modify it using Javafx. There is a modified one:

import java.io.FileNotFoundException;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.stage.Stage;

public class Bundesliga extends Application {
    Stage window;
    Scene scene1;
    public static void main(String[] args) throws FileNotFoundException {
        launch(args);
    }
    @Override
    public void start(Stage primaryStage) throws FileNotFoundException {
        PlayerManager league = new PlayerManager();
        window = primaryStage;

        Button button1 = new Button("Uruchom program");
        button1.setOnAction(e -> league.uruchom());

        StackPane layout = new StackPane();
        layout.getChildren().add(button1);
        scene1 = new Scene(layout, 600, 300);

        window.setScene(scene1);
        window.setTitle("Title here");
        window.show();
    }
}

I wanted to run my code by clicking on button, but when I'm trying to compile it, the error occours: "Bundesliga.java:24: error: unreported exception FileNotFoundException; must be caught or declared to be thrown button1.setOnAction(e -> league.uruchom()); I don't know how to solve that, because my methods throw FileNotFoundException. Please help me.

  • http://crunchify.com/better-understanding-on-checked-vs-unchecked-exceptions-how-to-handle-exception-better-way-in-java/ and http://stackoverflow.com/questions/6115896/java-checked-vs-unchecked-exception-explanation – kosa Jun 10 '16 at 16:00

1 Answers1

0

It seems that league.uruchom() is expected to throw a FileNotFoundException, but your lambda is overriding the actionPerformed method, which does not have this Exception in its signature.

You should handle the exception in the lambda's method body. At worst, you could convert the exception to a RuntimeException to handle later, but I don't recommend this.

Zircon
  • 4,677
  • 15
  • 32
  • Thank you, Could you tell me how I can handle the exception in the lambda's method body? I would be really grateful. – patroneq Jun 10 '16 at 17:54