Catch exceptions in javax.swing application

前端 未结 3 428
醉酒成梦
醉酒成梦 2021-01-12 19:41

I\'m working with javax.swing to make an aplication which generates forms from XML Schema (using JAXFront library) and stores the data filled by the user them i

3条回答
  •  情书的邮戳
    2021-01-12 19:54

    After you have called visualize() the only thread running is the Swing/AWT event dispatch thread. If you want to catch any exceptions you will need to do so in any of your listener methods that are called on this thread e.g.

    public void actionPerformed(ActionEvent e) {
      try {
        // Some code here
      } catch(RuntimeException e) {
        // Handling code here
      }
    }
    

    To prevent boilerplate you can have this code in a super class.

    Note that you can also set a default uncaught exception handler if you want to catch anything not already dealt with by the Swing/AWT thread.

    Note also that in general it is best practice to not catch subclasses of RuntimeException if you can avoid it.

提交回复
热议问题