JavaFX 8 (Lombard) global exception handling

前端 未结 2 1819
迷失自我
迷失自我 2021-02-10 09:50

I\'m currently trying to build an application with JavaFX 8, but I can\'t get uncaught exception handling to work. Due to this post (https://bugs.openjdk.java.net/browse/JDK-810

2条回答
  •  别那么骄傲
    2021-02-10 10:39

    James_D's answer helped me a lot, but I overlooked his comment. So in case others face the same issue, I post this as a complementary answer.

    In my case, my JavaFX app had to face and catch an Out Of Memory Error. For the same code sometimes the OOM appeared on the JavaFX main thread, sometimes not (could be "InvokeLaterDispatcher" thread, "Timer" thread, "process reaper" thread, ...), depending on which thread was first running out of memory. Consequently the error was not always caught and the app didn't always exit as required.

    Thus to be sure my app was logging this and exiting, I had to use Thread.setDefaultUncaughtExceptionHandler() as mentioned also by James_D in his comment:

    Thread.setDefaultUncaughtExceptionHandler((thread, throwable) -> {
        System.out.println(throwable.getClass() + " detected from default UEH.\nWill exit now");
        System.exit(1);            
    });
    

    Now the error is always caught when it appears.

提交回复
热议问题