Exception exception in javax.swing application

I work with javax.swing to create an application that generates forms from an XML schema (using the JAXFront library) and saves user-filled data in XML documents.

I put try-catch-finally blocks when I need it, but I have a slight problem with catching exceptions when the main thread ends (AWT threads are still running).

I have two classes that do the main work and other classes that are not important for the question:

  • Main class . It has the following structure. Initializes the application and launches the main frame

     public class Main { public static void main(String[] args) { readArgs(); // An INI file with the app config Model model = initializeElements(args); // My model class try { MyFrame mfr = new MyFrame(title,model); mfr.visualize(); // Assembling view and setting visible } catch( Excepion e ) { doCleanUp(); System.exit(-1); } } } 
  • Frame class : generates views and auditions

     public class MyFrame extends JFrame implements ActionListener,MenuListener { // Some attributes // Other mthods without importance /** * Compose the elements, add listeners and set visible the frame */ public void visualize() { generateFormPanel(); setListeners(); validate(); setVisible(true); } public MyFrame(String title, Modele model) { super(title); createElementsUsing(model); } public void actionPerformed(ActionEvent e) { // Code to manage events } } 

Well, the problem is this: When the render function is called from the main method, a view is created and displayed. At this point, I am losing control of catching exceptions. Then my question is, is there a way to catch the possible RuntimeExceptions thrown after this point .

I hope you understand my English and can answer the question.

Thanks in advance.

+6
source share
3 answers

The simplest version is to install a default exception handler:

 Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { public void uncaughtException(Thread t, Throwable e) { // do something } }); 

But this catches the fuzzy exceptions thrown into other parts of the program.

However, you can use only runtime exceptions thrown from the swing event dispatch thread using a proxy (see this page for more information, the copied code is from there):

 class EventQueueProxy extends EventQueue { protected void dispatchEvent(AWTEvent newEvent) { try { super.dispatchEvent(newEvent); } catch (Throwable t) { // do something more useful than: t.printStackTrace(); } } } 

Now install it like this:

 Toolkit.getDefaultToolkit().getSystemEventQueue().push(new EventQueueProxy()); 
+21
source

After you call visualize() , only the stream acts as a Swing / AWT send stream. If you want to catch any exceptions, you will need to do this in any of your listener methods that are called in this thread, for example.

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

To prevent using a template, you can get this code in a superclass.

Note that you can also set a default exception handler if you want to catch something that has not yet been processed by the Swing / AWT thread.

Also note that in general, it is better not to catch subclasses of RuntimeException if you can avoid this.

+1
source

Try adding:

 setDefaultCloseOperation(EXIT_ON_CLOSE); 

for the MyFrame constructor. Not sure though, but worth a try.

0
source

Source: https://habr.com/ru/post/893570/


All Articles