JavaFX 2 - Catching all runtime exceptions

后端 未结 3 683
傲寒
傲寒 2020-12-03 17:57

I tried

Thread.setDefaultUncaughtExceptionHandler...


in the main, and also in the start(Stage primaryStage) method. It ain\'t wor

3条回答
  •  难免孤独
    2020-12-03 18:24

    Setting the EventDispatcher to the root Node worked for me.

     public class Frame extends Pane {
        Frame() {
            setEventDispatcher(new EventDispatcher() {
    
                @Override
                public Event dispatchEvent(Event event, EventDispatchChain chain) {
                    try {
                        return chain.dispatchEvent(event);
                    } catch (final Exception e) {
                        // handle all the exceptions here 
                        return null;
                    }
                }
            });
        }
    }
    

提交回复
热议问题