Exception handling in Swing GUI

I am not sure how to manage exceptions in the GUI; my goal is to tell the user if something goes wrong, showing a clear message.

I am going to do something like this:

// I'm inside an actionPerformed() method try { // do whatever I have to do here } catch (KnownBusinessException1 e) { // inform the user and do something; // most times simply inform the user that it wasn't possible to complete the // operation and remain in the same window instead of moving forward. } catch (KnownBusinessException2 e) { // just as above } catch (KnownDataAccessException1 e) { // just as above } catch (KnownDataAccessException2 e) { // just as above } catch (RuntimeException e) { // I want to catch any other unexpected exception, // maybe NPE or unchecked IllegalArgumentExceptions and so on // something went wrong, I don't know where nor how but I will surely inform the user } 

Now: if the exceptions for catch were checked in the try block, would it be better to nest try / catch or catch these checked exceptions after catching a RuntimeException? (it probably depends, I don’t even know if this will happen, by the way)

Another thing: how about Error s? I wouldn’t want to experience an unexpected shutdown, if I were a user, I would prefer the application to tell me that something went wrong, and no one can do anything about it, "the end of the world is coming, so I’ll go straight now". At least I would know that it was not my fault lol.

Btw doesn't know if it's good practice to catch bugs ...: \

Is there a better way to do this in a Swing app?

+6
source share
3 answers

I think it is best to explicitly catch all checked exceptions and install an exception handler for the rest. See So: How can I detect when an exception was thrown globally in Java?

This is how I use Thread.setDefaultUncaughtExceptionHandler:

 public static void setupGlobalExceptionHandling() { Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(Thread t, Throwable e) { handleException(e); } }); } 

Note that the "sun.awt.exception.handler" trick for the EDT thread mentioned in many SO posts is not required and does not work in Java 7. For Java 7, just use the standard Thread.setDefaultUncaughtExceptionHandler, as described above. Of course, if you use both mechanisms to register an exception handler, the code will work in all versions.

BTW, the EDT thread automatically restarts if an uncaught exception is thrown (but your application may remain in an inconsistent state), see the following: EDT and exception at runtime

+10
source

If exceptions for catch were checked in the try block, would it be better to nest try / catch or catch these checked exceptions after catching a RuntimeException? (it probably depends, I don’t even know if this will happen, by the way)

As you said, it depends on whether it makes sense to execute the rest of the code in the try block after the exception. If not, then it makes no sense to embed try / catch blocks.

0
source

A good way to show that the user has acted incorrectly is to use JOptionPane s. Add to that the good use of the icons (info / error) and you're good to go. Here is a sample code for your reference:

http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html

You can consider some customization / abstraction classes over JOptionPane if you want :)

As for handling multiple exceptions in the same way, if the message is the same in all 3 KnownBusinessException and KnownDataAccessException , then you can make sure that both classes have the same origin and catch the same class. If the same handling is required for KnownBusinessException and not KnownDataAccessException s, all KnownBusinessException with the same parent and all KnownDataAccessException with the same parent. I hope you understand where I am going with this.

0
source

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


All Articles