Gui application exception handling

I lost a bit how to handle thrown exceptions in my graphics application.

I, for example. have a function that saves the company created by the user in a (built-in) database.

The save function of a newly created company throws 3 Exceptions:

IllegalArgumentException : If the company or non-empty field is NULL (manually checked and selected).

EntityExistException : If the company (this name) already exists. (Also checked manually and throws).

PersistenceException : If something went wrong when trying to save. (Hooked and abandoned).

The function that calls the saveCompany method catches all 3 Exceptions, and then writes them to the log and shows the user a message that an error has occurred.

Now I wonder if they need to be caught at all? Or would it be ok to just let them run to globalExceptionHandler (where can I also see them)? And they are also interested in what should be my reaction?

Do I have to tell the user that an error has occurred and let the program start (because other parts of the program must function properly), or should I tell him and then end the program (cause a programmer error that should not be there)?

+4
source share
3 answers

In the case of IllegalArgumentException, you should catch the exception and ask the user to correct the data (do not print stacktrace).

In the case of an EntityExistException, the user should be informed that the company already exists, and perhaps he should consider updating it.

When the user receives a PersistenceException , they should be presented with a dialog box with stacks (and possibly other data related to the developer), and report an error report being sent.

+4
source

So the good news is that you are asking all the right questions.

Do I have to tell the user that an error has occurred and let the program start (because other parts of the program must function properly), or should I tell him and then end the program (cause a programmer error that should not be there)?

This is a design issue that you should think carefully about. If this is a recoverable error and the program cannot continue to work, the program should exit if the user does not have this parameter. If part of the program must die, but other parts can go alone, the user must be informed. If the user needs to correct some data so that the program can run, the user should be informed as such. Etc. Yes, you are asking the right questions, although you just need to think about them and be reasonable.

+4
source

In my opinion, do the following

EntityExistException : EntityExistException user that the object already exists. Continue to work with the application.

PersistenceException and IllegalArgumentException : provide the user with a generic message and stop the application.

Hopefully you will see a difference in how the above two exceptions are handled. One of them can be called and corrected by the user. Another user cannot do anything.

+1
source

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


All Articles