Question

I'm a little lost on how to handle unchecked exceptions in my GUI application.

I e.g. have a function that saves a company newly created by the user in a (embedded) database.

The function for saving the newly created company throws 3 Exceptions:

IllegalArgumentException: If the company or a not null field is null (Manually checked and thrown).

EntityExistException: If the company (it's name) already exists. (Also manually checked and thrown).

PersistenceException: If something went wrong when trying to save. (Catched and rethrown).

The function that calls the saveCompany method catches all 3 Exceptions and then logs them and shows a dialog to the user that an error had occurred.

Im now wondering if i need to catch them at all? Or would it be ok to just let them run up to the globalExceptionHandler (where i can also look them)? And im also wondering what my reaction should be?

Should I tell the user that there was an error and let the program run (cause other parts of the program should function properly) or should I tell him and then end the program (cause it's a programmers error that shouldn't be in there)?

Was it helpful?

Solution

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

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

When the user receives the PersistenceException they should be presented with a dialog window with the stacktrace (and perhaps other data relevant for the developer) and informed to submit a bug report.

OTHER TIPS

So the good news is you're asking all the right questions.

Should i tell the user that there was an error and let the program run (cause other parts of the program should function properly) or should i tell him and then end the program (cause it's a programmers error that shouldn't be in there)?

That is a design question you need to think carefully about. If it is a recoverable error and there is nothing the program can do to continue running, then the program should shutdown without the user having the option. If part of the program must die but other parts may go one, the user should be informed. If the user needs to fix some data so the program can run, the user should be informed as such. Etc. Yes, you are asking the right questions though, you just do actually have to think about them and be judicious.

In my opinion , do the following

EntityExistException : Let user know that entity already exists. Continue with the app.

PersistenceException and IllegalArgumentException : Give user a generic message, and stop the app.

I hope you see the difference in how the above two exceptions are being handled. One is something that can be caused and fixed by the user. The other is something the user cannot do anything about.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top