0

I created "initialize" method automatically, out of "implements Initializable" class. After that, I added simple code to load Menu Screen designed in Scene Builder. Part of this code requires handling an exception (IOException). However, when I try to simply add "throws IOException" to the method itself, it gives me an error that reads:

'initialize(URL, ResourceBundle)' in 'com.javafx.controllers.RootController' clashes with 'initialize(URL, ResourceBundle)' in 'javafx.fxml.Initializable'; overridden method does not throw 'java.io.IOException'

I tried it with other "initialize" method made by myself and this error doesn't occur anymore. I would be glad if someone could explain this...

public class RootController implements Initializable {

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) throws IOException {
        loadMenuScreen();
    }

    public void initialize() throws IOException {
        loadMenuScreen();
    }

    public void loadMenuScreen() throws IOException {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("/com/javafx/screens/MenuScreen.fxml"));
        Pane pane = loader.load();
        MenuController menuController = loader.getController();
        menuController.setRootController(this);
        setScreen(pane);
    }

The error occurs at the first initialize but not at the second. I understand that it has probably something to do with (URL url, RescourceBundle resourceBundle) but I don't understand how...

  • Overridden methods cannot declare checked exceptions in the throws clause if the original method does not itself declare the same checked exception or a supertype in _its_ throws clause... – Slaw Dec 16 '19 at 10:50

1 Answers1

0

An overriding method can throw any uncheck exceptions, regardless of whether the overridden method throws exceptions or not. However the overriding method should not throw checked exceptions that are new or broader than the ones declared by the overridden method. The overriding method can throw narrower or fewer exceptions than the overridden method.