FXMLLoader.load(url)引发的IOException不会被捕获

时间:2019-09-11 18:01:05

标签: java exception javafx-8 fxmlloader

我正在使用javaFX应用程序,我想在应用程序启动时崩溃时运行一些代码,因此基本上是在start()方法周围的catch子句的FXMLLoader.load()方法中,但是由于某种原因,它赢了没抓住它抛出的IOException。(我在getResource()方法中输入了错误的url,从而引发了异常)

但是,如果我将IOException与基类Exception交换了,那么它起作用了,所以在我不知道的Exception之前抛出了一个更深的嵌套IOException ? (控制台显示java.lang.reflect.InvocationTargetException

更新: 我尝试将getResource()FXMLoader.load()函数分开,并用一条消息将它们分开,以查看将Exception放在哪里。 结论:Exception方法抛出了FXMLLoader.load() 100%。

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");
    }
}

堆栈跟踪:

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

1 个答案:

答案 0 :(得分:0)

引发的异常是NullPointerException,它不是IOException的子类,这就是未捕获异常的原因。

NullPointerExceptionRuntimeException,因此,如果您想捕获NullPointerException,请捕获NullPointerExceptionRuntimeException

由于Exception扩展了RuntimeException,因此在您使用Exception时捕获了异常原因。

要捕获此特殊异常,您可以执行

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

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

也不会从您的代码中抛出java.lang.reflect.InvocationTargetException,而是从JavaFX抛出它,后者​​调用您的public void start(Stage primaryStage),因为有一个异常JavaFX将原始异常包装在java.lang.reflect.InvocationTargetException