Search code examples
javaexceptionjavafx-8fxmlloader

IOException thrown by FXMLLoader.load(url) won't be catched


Im working on javaFX app and i want to run some code when the app crashes on startup so basically in the start() methode in the catch clause that surrounds the FXMLLoader.load() methode but for some reason it won't catch the IOException thrown by it.(I put a wrong url in the getResource() methode so that the exception is thrown)

But if i exchange IOException with the base class Exception it works, so is there a deeper nested Exception thrown before the IOException that i don't know about ? (the console displays a java.lang.reflect.InvocationTargetException)

Update: I tried separating the getResource() and the FXMLoader.load() function and seperated them with a message to see where the Exception is thrown. Conclusion : the Exception is 100% thrown by the FXMLLoader.load() methode.

public void start(Stage primaryStage) {

    stage = primaryStage;

    try {
        URL url = getClass().getClassLoader().getResource("view/Lgin.fxml");
        System.out.println("URL loaded");
        Parent root = FXMLLoader.load(url);
        System.out.println("FXML loaded");
        stage.setScene(new Scene(null));
        stage.getIcons().add(new Image("resources/icons/Login.png"));
        stage.setTitle("main");
        stage.setResizable(false);
        stage.centerOnScreen();
        stage.show();
    } catch(IOException e) {
        //LOGGER.log(Level.SEVERE, e.toString(), e);
        //System.exit(0);
        System.out.println("catch reached");
    }
}

Stack trace :

URL loaded
Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at launcher.DigitalCourt.start(DigitalCourt.java:42)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    ... 1 more
Exception running application launcher.DigitalCourt
Java Result: 1

Solution

  • The exception thrown is NullPointerException which is not a subclass of IOException that's the reason the exception is not caught.

    NullPointerException is a RuntimeException so if you want to catch NullPointerException either catch NullPointerException or RuntimeException

    The reason for the exception is caught when you use Exception because RuntimeException extends Exception.

    To catch this particular exception you can do either

    catch(IOException|NullPointerException e) {
        //LOGGER.log(Level.SEVERE, e.toString(), e);
        //System.exit(0);
        System.out.println("catch reached");
    }
    

    or

    catch(IOException|RuntimeException e) {
        //LOGGER.log(Level.SEVERE, e.toString(), e);
        //System.exit(0);
        System.out.println("catch reached");
    }
    

    Also java.lang.reflect.InvocationTargetException is not thrown from your code instead it is thrown from JavaFX which call your public void start(Stage primaryStage) since there is a exception JavaFX wraps the original exception inside java.lang.reflect.InvocationTargetException