0

In the following code, I have used a wrong file name (wrongFileName.fxml) to get loaded, in order to test catch block. However, the statements inside the catch block doesn't execute. Please note, even when there is no statement to print the stack trace on the console, still stack trace gets printed. Please let me know, why this is happening?

public class First extends Application {

    @Override
    public void start(Stage stage){
        Parent root = null;
        try {
            root = FXMLLoader.load(getClass().getResource("wrongFileName.fxml"));
        } catch (IOException ex) {
            System.out.println("SomeTextForCheck");
        }
    }
}

Stack trace:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.javafx.main.Main.launchApp(Main.java:698)
    at com.javafx.main.Main.main(Main.java:871)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403)
    at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
    at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
    at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2773)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2757)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2743)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2730)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2719)
    at first.First.start(First.java:29)
    at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
    at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:216)
    at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
    at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java:89)
    ... 1 more
Vaib
  • 103
  • 3
  • 11
  • 1
    how if you use `Exception` instead `IOException` ? – Daniel Robertus Jan 13 '14 at 02:47
  • have you tried `catch (Exception ex)` instead of `catch (IOException ex)`? – Baby Jan 13 '14 at 02:47
  • Is that really throwing IOException? Otherwise if you catch the Exception class itself, it will helps you. – Venkatesh Achanta Jan 13 '14 at 02:50
  • catch (Exception ex) prints out the text in System.out.println(), but also prints Stack trace. Is it how it should be? Thanks! – Vaib Jan 13 '14 at 02:50
  • [This answer to another question](http://stackoverflow.com/questions/12318861/javafx-2-catching-all-runtime-exceptions/12798390#12798390) may explain where the exception gets caught and where the stacktrace gets printed. – fabian Jan 13 '14 at 03:00

2 Answers2

0

From the stack trace, RuntimeException is thrown. So Kindly handle the RuntimeException and Exception class in your code. Always log the exception or use stacktrace otherwise its difficult to debug.
public class First extends Application {

@Override
public void start(Stage stage){
    Parent root = null;
    try {
        root = FXMLLoader.load(getClass().getResource("wrongFileName.fxml"));
    } catch (IOException ex) {
        System.out.println("SomeTextForCheck");
        ex.printStackTrace() 
    }
    catch(RuntimeException ex) {
        System.out.println("SomeTextForCheck");
        ex.printStackTrace()
    }
    catch(Exception ex) {
        System.out.println("SomeTextForCheck");
        ex.printStackTrace()  
    }
}
0

You need to catch the right exception to print the stack trace. Ideally you shouldn't be getting exceptions like NullPointerException, developers need to handle such conditions in the code.

Sudhanshu Umalkar
  • 4,174
  • 1
  • 23
  • 33