How do I catch this exception in Swing?

后端 未结 5 1916
滥情空心
滥情空心 2021-01-02 09:09

I have a Swing application, and even though I have everything in a try/block, the exception isn\'t caught.

public static void main(         


        
相关标签:
5条回答
  • 2021-01-02 09:33

    The only suitable ways that I am aware of, in order to catch exceptions thrown from inside the EDT are:

    • write your own EventQueue (I woudln't advise it in general)
    • use Swing internal property "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.

    0 讨论(0)
  • 2021-01-02 09:38

    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.

    0 讨论(0)
  • 2021-01-02 09:50

    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:

    • put a try/catch around the actual code where the exception is occurring: e.g. if it's in response to a button click handled by an ActionListener, put the try/catch inside your actionPerformed() method;
    • or, leave the exception as an uncaught exception, and add an uncaught exception handler. For example:
    
        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().

    0 讨论(0)
  • 2021-01-02 09:52

    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.

    0 讨论(0)
  • 2021-01-02 10:00

    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.

    0 讨论(0)
提交回复
热议问题