I have a Swing application, and even though I have everything in a try
/block
, the exception isn\'t caught.
public static void main(
The only suitable ways that I am aware of, in order to catch exceptions thrown from inside the EDT are:
sun.awt.exception.handler
" (I use
it and it works on all Sun JDK 1.4,
1.5 and 1.6 at least, plus on IBM JDK 1.4 and 1.5 at least; I didn;t check it on other JDK though)You should take a look at this thread to have a more complete overview of the solutions with their pros and cons.
Swing runs things in the event dispatching thread. You are trying to catch it in the main thread.
And note that swing is not thread safe, you too should be doing things in event dispatching thread.
To catch the exception, you can override some method from that stack trace, like the paint method from your component.
And for me that exception does look like a bug you should fix, not something you should hide by catching.
As mentioned by another poster, your problem is that the exception is being thrown in another thread, the event dispatch thread. A couple of solutions:
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
public void uncaughtException(Thread t, Throwable e) {
// ... do something with exception here ...
}
});
On a side-note, you should in principle but your UI startup code in a SwingUtilities.invokeLater().
As mentioned above, the problem is where the exception is being thrown - on the event dispatch thread.
If you want to set up a try/catch block to catch this particular problem, I would throw one into the App class's paint method. Override it and put a call to super.paint in a try catch block there.
If you want a generic way to catch uncaught exceptions, take a look at Thread.setUncaughtExceptionHandler. You call that method with an exception handler and you can deal with all the exceptions which don't get caught in your application.
Runtime exceptions like ArrayIndexOutOfBoundsException shows a programmer mistake. So it might be better to fix them rather catching and silently chewing it.
Just a wild guess for the cause of exception. Something concurrently remove rows from the table model's datavector once the JTable starts to draw the data on screen.