Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error handling in a GUI

 
Dennis Grimbergen
Ranch Hand
Posts: 159
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I need a nice way to handle errors in a Swing GUI. Imagine a two-layered application (gui and database). A user clicks a button in the GUI, after which the database performs some action. Let's say a DatabaseException is thrown in the database code. Because I want to show a user friendly exception, I would assume something like this (in the GUI):

From here it's not hard to show this thrown Exception in a alert box or something like that.

While searching the internet for some nice ways I noticed that something like this seems to be common?

Is this a correct way to deal with this? To me it seems not correct to use a static method (showError), from an OO perspective.
 
Karthik Shiraly
Bartender
Posts: 1208
25
Android C++ Java Linux PHP Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The design I usually follow is:

- translate API and framework exceptions to my own application defined (and related to application use cases) exceptions. For example, IOException to DiaryLoadException or DiarySaveException

- propagate these app defined exceptions all the way up so that other layers can take corrective actions if any (may help with requirements yet to come)

- At the highest layer of the application, catch and either log the exception, or pass it onto a view class for display. The display class uses the details in the exception (an integer ID is common in my apps) to form a locale specific error message and display it.

Can't say which approach is 'correct' because it's subjective. The above design has helped keep my apps flexible and extensible.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic