GUI thread exception handler

65 views
Skip to first unread message

vasso123

unread,
Jul 21, 2018, 9:47:07 AM7/21/18
to lanterna-discuss
Hi guys,

I'm having a problem regarding the exception handler on the TextGUIThread.
As I understand it, setting the exception handler using the setExceptionHandler method, all unhandled exceptions in the "user" code are trapped and handled with that handler.
However, I can't get it to work, example:
package hr.vkeglevic.doomsdayterminal;

import com.googlecode.lanterna.gui2.BasicWindow;
import com.googlecode.lanterna.gui2.Button;
import com.googlecode.lanterna.gui2.MultiWindowTextGUI;
import com.googlecode.lanterna.gui2.TextGUIThread;
import com.googlecode.lanterna.screen.TerminalScreen;
import com.googlecode.lanterna.terminal.DefaultTerminalFactory;
import com.googlecode.lanterna.terminal.Terminal;
import java.io.IOException;

public class TestExceptionHandler {

   
private static DefaultTerminalFactory terminalFactory;
   
private static Terminal terminal;
   
private static TerminalScreen screen;
   
private static MultiWindowTextGUI textGUI;
   
private static BasicWindow window;

   
public static void main(String[] args) throws IOException {
        terminalFactory
= new DefaultTerminalFactory();
        terminal
= terminalFactory.createTerminal();
        screen
= new TerminalScreen(terminal);
        screen
.startScreen();
        textGUI
= new MultiWindowTextGUI(screen);
        setExceptionHandler
();
        window
= new BasicWindow();

       
Button button = new Button("test");
        button
.addListener((b) -> {
            setExceptionHandler
();
           
throw new RuntimeException("This should be caught in the uncaght exception handler!");
       
});
        window
.setComponent(button);

        textGUI
.addWindowAndWait(window);
        screen
.stopScreen();
   
}

   
private static void setExceptionHandler() {
        textGUI
.getGUIThread().setExceptionHandler(new TextGUIThread.ExceptionHandler() {

           
private boolean handleException(Exception e) {
               
System.err.println("### Caught!");
                e
.printStackTrace();
               
return false;
           
}

           
@Override
           
public boolean onIOException(IOException e) {
               
return handleException(e);
           
}

           
@Override
           
public boolean onRuntimeException(RuntimeException e) {
               
return handleException(e);
           
}
       
});
   
}

}

When pressing the button, the string "### Caught!" should be printed on the standard output, but it doesn't.

Any ideas?

Thanks

Martin Berglund

unread,
Jul 29, 2018, 10:26:01 AM7/29/18
to lanterna...@googlegroups.com
Hello,
I don't have the code open right now, did you try to step through with a debugger and see what happened to the exception? The code should be pretty clear if you download the sources from Maven central from your IDE.

Regards,
Martin

--
Det här meddelandet skickas till dig eftersom du prenumererar på gruppen "lanterna-discuss" i Google Grupper.
Om du vill sluta prenumerera på den här gruppen och inte längre få någon e-post från den skickar du ett e-postmeddelande till lanterna-discu...@googlegroups.com.
Fler alternativ finns på https://groups.google.com/d/optout.

vasso123

unread,
Jul 30, 2018, 4:52:49 PM7/30/18
to lanterna-discuss
Hi,

tried just now, here's the stack trace:

at hr.vkeglevic.test.TestExceptionHandler.lambda$main$0(TestExceptionHandler.java:32)
at com.googlecode.lanterna.gui2.Button.triggerActions(Button.java:101)
at com.googlecode.lanterna.gui2.Button.handleKeyStroke(Button.java:93)
at com.googlecode.lanterna.gui2.AbstractInteractableComponent.handleInput(AbstractInteractableComponent.java:142)
at com.googlecode.lanterna.gui2.AbstractBasePane.doHandleInput(AbstractBasePane.java:126)
at com.googlecode.lanterna.gui2.AbstractBasePane.handleInput(AbstractBasePane.java:97)
at com.googlecode.lanterna.gui2.AbstractWindow.handleInput(AbstractWindow.java:133)
at com.googlecode.lanterna.gui2.MultiWindowTextGUI.handleInput(MultiWindowTextGUI.java:333)
at com.googlecode.lanterna.gui2.AbstractTextGUI.processInput(AbstractTextGUI.java:94)
at com.googlecode.lanterna.gui2.AbstractTextGUIThread.processEventsAndUpdate(AbstractTextGUIThread.java:75)
at com.googlecode.lanterna.gui2.MultiWindowTextGUI.waitForWindowToClose(MultiWindowTextGUI.java:406)
at com.googlecode.lanterna.gui2.AbstractWindow.waitUntilClosed(AbstractWindow.java:269)
at com.googlecode.lanterna.gui2.MultiWindowTextGUI.addWindowAndWait(MultiWindowTextGUI.java:370)
at hr.vkeglevic.test.TestExceptionHandler.main(TestExceptionHandler.java:36)

Exception went uncaught through the lanterna code.
Using lanterna 3.0.0.

Pordi Erstaquel

unread,
Jul 31, 2018, 8:51:40 PM7/31/18
to lanterna-discuss
Why are you setting the exception handler each button click? Probably not a solution, but this looks to me error-prone.

vasso123

unread,
Aug 1, 2018, 6:14:06 AM8/1/18
to lanterna-discuss
Just to prove that it doesn't matter when you set the exception handler (before or after starting running the gui thread).
Reply all
Reply to author
Forward
0 new messages