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