How to handle Java exception in a user-friendly way

I am developing a swing application, and I'm a little confused how I can handle exceptions, for example, recently, part of my code has been renaming files. Therefore, when I tested it, I came up with โ€œyou do not have permission to rename the fileโ€, as I received it from the print exception message. So how can I express this message to the user? and should I use a JOptionPane message or just show it in the status bar?

thanks

+7
java exception exception-handling swing
source share
7 answers

From your question, it looks like you already know how to handle an exception in the sense of java. However, you are looking for tips on how to respond to exceptions as soon as you catch them.

In the specific example that you give in your question, I (as a user) would like this error to be presented quite clearly, so JOptionPane may be your best choice. I would not just update the status bar, as this is very close to a silent failure, and the user will just be confused.

A personal rule of thumb is that if the user seems to be waiting for code to complete before continuing with his task, then they should be informed of the failure, i.e. modal field. If there is a failure in the background task that the user can perform without worries, or the code can restore it, or the code will try again, then I would go with a more subtle approach to the status bar or change the icon depending on the user interface.

+8
source share

To comment on Kevin D's comment, it really depends on your expected user audience. If they are technically competent, you can use the exception text as is. If not, I would prefix the message with "An error occurred, contact your technical support staff with the following information:" then add an error message and, ideally, a unique identifier to search for a related journal entry ... I often use a timestamp for this.

If you really want to get some imagination, you can email technical support technicians who have much more detailed information, for example, about an exclusive and complete stack trace, a copy of a journal entry, etc. I have done this in the past, but you have to be careful, a frequently occurring error will quickly fill the mailbox :)

Of course, if the error can be corrected by the user, then you can simply say this (and how to do it) in your message. It is about as thorough and interesting as you can get ...

+2
source share

I do not quite understand the first part of your question, but I'm trying to answer the second. In general, how you want to display the error for the user depends on the software and the error. In most cases, a JOptionPane or similar is suitable. However, in some cases, the modal dialog may be too intrusive, and the status bar may be the best way. But then again, it depends on what kind of software you write.

+1
source share

If you expect an exception to occur as a result of a userโ€™s action, you must explicitly catch it at a point that makes sense and guarantees the correct operation of your program.

For example, if a user can rename a file, you can call the rename () method, which returns a status code to indicate success or a failure error code. Inside the method, one of these codes may indeed be called by an exception, but the call code does not care. Once the call returns, a status code can be used to determine which error message will be displayed (if any).

  enum RenameStatus { Success, Failed, SecurityFailed } void doRename(File fromFile, File toFile) { switch (rename(fromFile, toFile)) { case Success: break; case Failed: // todo generic dialog rename operation failed break; case SecurityFailed: // todo security dialog rename operation failed due to permission break; } } RenameStatus rename(File fromFile, File toFile) { try { if (!fromFile.renameTo(toFile)) { return RenameStatus.Failed; } return RenameStatus.Success; } catch (SecurityException e) { // Error - permission error return RenameStatus.SecurityFailed; } catch (Exception e) { return RenameStatus.Failed; } } 
+1
source share

If you catch an expression (enclosed in a try-catch block), you will be notified when this exception occurs. Then you need to decide: is there a way to make everything work again? Could you ask the user a different file name? Then do it! But if there is no reasonable way to work around the error, just stop the program.

0
source share

File permissions are a "normal" exception, not an "exceptional" one, since there will be a "full disk", so you probably just use JOptionPane instead of sending an error report. However, some of the earlier answers are very informative and should be accepted for general cases.

In addition, my main() always starts with this:

 Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() { public void uncaughtException(Thread t, Throwable e) { // do your things: see earlier answers } } 
0
source share

Use Try-Catch Processing ...

http://tutorials.jenkov.com/java-exception-handling/basic-try-catch-finally.html

When you catch an exception, you can do whatever you want with it. Display it to the user, do something else in the code, display another message to the user based on the exception, etc.

-one
source share

All Articles