I make an interpreter of the Prolog language. The language is based on recursion, and incorrectly written programs often lead to stack overflow. When I ran my interpreter from a JavaFX application, I noticed that JavaFX could detect a StackOverflowError in the stream and process it.

How do you do something like this yourself to determine the StackOverflowError thrown in another thread and process it?

StackOverflowError in stacktrace

    1 answer 1

    In general, I would advise this task (intercepting the expected exception) on the stream itself, but if this is not possible for any reason:

    @Override public void start(Stage primaryStage) { Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler()); 

    See setDefaultUncaughtExceptionHandler , UncaughtExceptionHandler

    Edit (after providing source codes):

    Inside the installed UncaughtExceptionHandler :

     Platform.runLater(() -> errorsOutput.runtimeException(new RuntimeException("Error in program thread", throwable))); 
    • I also found these methods, tried using them, but JavaFX still caught the error instead of my listener. I even tried to set a new group for a thread, anyway, JavaFX intercepts it somehow and the program hangs. Where is the connection between the thread that I create and javafx, I do not understand. - Maxim Falaleev
    • Create a small application on which this situation is reproduced (along with this listener). So it will be easier to help. - Alexander Savostyanov
    • Strange, I created a simple javafx project with recursion in the stream and a listener, everything works. And my project is almost the same, but it does not work. We still have to throw your project. github.com/FalaleevMaxim/Prolog-IDE Listener in the Controller class on line 145. Enter and run such an application in the application: predicates p clauses p:-p. goal p predicates p clauses p:-p. goal p - Maxim Falaleev