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?
source share