DEV Community

Mark Rubin
Mark Rubin

Posted on

What's A Good Error Message

This is part of a brief series on Good Habits For Java Programmers.

As you progress in your programs, you'll start to handle error conditions. Maybe you'll prompt your users to input a value for the side of a square, and they type a negative value or a string. There's the whole programming side of how you detect those error conditions, but that's not what this post is about. This post is about how to surface those errors.

Error messages should be specific and actionable

Let's say you have a UI where you ask your user to enter in a date of birth and a name. The user can enter in an invalid value for any of those. Tell them specifically what they did wrong and suggest what they can do to fix it. If they've made multiple errors, tell them about the multiple errors.

Let's say you require that the name not be empty and you expect your dates to be entered in the format MM/dd/yyyy (two digit month followed by a slash, followed by a two digit day followed by a slash, followed by a four digit year). The user enters in some data, and they use an improper date format: they enter "5/14/70". An error message saying "Something went wrong" or "Invalid input" is not helpful. What went wrong? What should they fix? Should they fix their name or their date? And what's wrong with their name or their date? Tell them their date is in an incorrect format and what format you expect. It's frustrating as a user to have no idea how to fix whatever seems to be broken with the data they entered.

Tell them about all their errors at once, if you can

Let's say they enter an incorrectly formatted date and left the name field blank. Tell them about both errors. It's frustrating for a user to fix one error and then resubmit their data and be told there are more errors.

Don't use computer jargon in your error messages

When you're learning to program, you don't really have users: usually, there's you, the programmer pretending to be a user, and your instructor. And both of you are well versed in computer jargon. But you're practicing writing programs for other people. And other people are usually not Java programmers. So don't say something like "Price is incorrectly entered. Please enter a float." as your error message. Don't expect your user to know what a float is.

Error messages should be grammatical, and usually should be complete sentences

Anything else puts a burden on your user to interpret what you must have meant. Grammatical, clear, full sentences are preferred because when you use them, your users will have the best chance of understanding what went wrong and how to fix it.

Double check your error messages

I see a lot of mistakes from beginning programmers that come from copy and pasting. They copy an error message about expecting an integer input to the place where they let a user know about an error where a string is expected, and so they tell the user that an integer is expected when really it's a string that's expected. We all do this - not just beginning programmers. But it's something we should all be reviewing to try to catch on our own.

Top comments (0)