Hi,
I decided to sit down over the past couple of days and figure out a way to handle all exceptions that could come out of my rendering code in such a way that I could get a little bit of feed back in a dialog. I’ve never really had to mix swing EDT things with error handling and jogl things before so I’ve been hitting some rough bits and was looking for advice.
My first approach was to do something like this:
@Override
public void display(GLAutoDrawable glDrawable) {
if (exceptionDialog != null) {
if (exceptionDialog.isFinished()) {
System.exit(1);
}
} else {
try {
gl = glDrawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
for (Renderer r : currentContext.getRenderers()) {
camera = r.getCamera();
r.initialize(this);
r.renderBefore(this);
r.render(this);
r.renderAfter(this);
}
}
} catch(Throwable t) {
exceptionDialog = new ExceptionDialog(t);
exceptionDialog.launch();
}
}
Actually that is my second attempt. My first was to make the dialog modal but I ended up having the swing EDT keep running (I Think?) and it would keep hitting display and keep hitting the exception. With this approach it will keep calling display, but if there is a dialog it won’t do my rendering stuff.
So, eventually I learned about the setUncaughtExceptionHandler on Thread objects and decided I kind of liked that instead of having a try-catch block so now I have:
@Override
public void display(GLAutoDrawable glDrawable) {
if (exceptionDialog.isRunning()) {
if (exceptionDialog.isFinished()) {
System.exit(1);
}
} else {
gl = glDrawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
for (Renderer r : currentContext.getRenderers()) {
camera = r.getCamera();
r.initialize(this);
r.renderBefore(this);
r.render(this);
r.renderAfter(this);
}
}
}
@Override
public void init(GLAutoDrawable glDrawable) {
exceptionDialog = new ExceptionDialog();
Thread.currentThread().setUncaughtExceptionHandler(exceptionDialog);
So my question now becomes: Is this the right approach? Is setting an exception handler on the EDT dangerous?
On a somewhat related but not quite so much note: is there a way to completely stop the canvas from being updated so that when I catch an exception I stop the canvas and don’t have to do checking in my display for the exception dialog? A quick and dirty (it seems) way to do it is to remove the canvas from the window.
Thank you very much,
Eugene