views:

108

answers:

7

I hate writing error condition code. I guess I don't have a good approach to doing it:

  • Do you write all of your 'functional' code first then go back in and add error handling or vice versa?
  • How stupid do you assume your users are?
  • How granular do you make your exception throws?

Does anyone have sage advice to pass on to make this easier?

A lot of great answers guys, thank you. I had actually gotten more answers about dealing with the user than I thought. I'm actually more interested in error handling on the back end, dealing with database connection failures and potential effects on the front end, etc. Keep them coming!

+3  A: 

Assume your users don't know anything and will break your system any way that it can possibly be broken.

Then write your error handling code accordingly.

Jason
A: 

You have to assume your users are incredibly stupid. Someone will always find a way to give you input that you thought would never happen.

I try to make my exception throws as granular as possible to provide the best feedback when something goes wrong. If you lump everything together, you can't tell which error case caused the problem.

I usually try to handle error cases first (before getting functional code), but that's not necessarily a best practice.

Sapph
+7  A: 

I can answer one question: You don't need to assume your users are "stupid", you need to help them to use your application. Show nice prompts for things, validate data and explain why, so it's obvious to them, don't crash in their face if you can't handle what they've done (or more specifically, what you've let them do), show a nice page explaining what they can do instead, and so on.

Treat them with respect, and don't assume they know everything about your system, you are here to help them.

In respect to the first part; I generally write most error-handling at the time, and add a little bit back in later.

I generally don't throw that many exceptions.

Noon Silk
Sounds entirely too altruistic to me. :p
Nathan Taylor
+1; "your" system tries to solve a user problem. If its create another problem, can't be a good system.
Rubens Farias
Correct, you need to help them use your application. A crucial part of being able to help users is to catch every possible error in the most meaningful way, this means assuming that everything that can fail, will fail and you better prepare and handle it correctly. Else all you will be able to do for your users is show a popup saying 'Unexpected error. Application will now quit".
Vinko Vrsalovic
I agree. In general, avoid throwing exceptions by validating your data first. But if you do throw an exception, make sure that it makes sense. Which one is a user more likely to fix: "Unable to create XML serialization file" or "Filename must only contain [XYZ] characters"? Exceptions are for exceptional things but if you want your users to be able to do something with it they have to make sense.
Chris Haas
+1  A: 

First, and foremost, be clear to the user on what you expect. Second, test the input to verify it contains data within the boundaries you expect.

Prime example, I had a form with an email field. We weren't immediately using that data so we didn't put any checking on it. Result: about 1% of the users put in their home address. The field was labeled "Email Address" Apparently the users were just reading the second word and ignoring the first.

The fix was to change the label to simply say "Email" and then test the input. For kicks we went ahead and logged what the users were initially typing in that field just to see if the label change helped. It did.

Also, as a general practice, your functions should test the inputs to verify they contain the data you expect. Use asserts or their equivalent in your language of choice.

Chris Lively
+1  A: 

When i code, there will be some exceptions which i will expect, i.e. a file may be missing, or some xml serialisation may fail. Those exceptions i know will happen ahead of time, and i can put in handling for them.

There is a lot you cannot anticipate though, and nor should you try to. Put in a global error handler and logger, so that ultimately everything gets caught and logged. Then as your testers and/or users find situations that cause exceptions (i.e. bad input) then you can decide whether you want to put further handling in specifically for it, or maybe leave it as it was.

Summary: validate your input, but don't try to gaze into the crystal ball too much, as you will never anticipate every issue that the user may come up with. Have a global handler and logger, and then refine as necessary.

slugster
I really like the idea of a global catchall just in case some oddball exception pops up eventually.
James
A: 

Someone has already mentioned defensive programming. A few thoughts from a user experience perspective, though.

  • If the user's input is invalid, either (a) correct it if you're reasonably sure you knew what they meant or (b) display a message in line that tells them what corrective action they should take.
  • Avoid messages like, "FATAL SYSTEM ERROR CODE 02382981." Users (a) don't know what this means, even if you do, and (b) are intimidated and put off by seeing things like this.
  • Avoid pop-up messages for every—freaking—possible—error you can come up with. You shouldn't disrupt user flow unless you absolutely need them to resolve a problem before they can do anything else.
  • Log, log, log. When you show an error message to the user, put relevant information that might help you debug in either (a) a log file or (b) a database, depending on the type of application you're creating. This will ease the effort of hunting down information about the error without making the user cry.

Once you identify what your users should and should not be able to do, you'll be able to effectively write error handling code. You can make this easier on yourself with helper methods/classes.

In terms of your question about writing handling before/after/during business logic, think about it this way: if you're making 400,000 sandwiches, it might be faster to add all the mustard at the same time, but it's probably also a lot more boring than making each sandwich individually. Who knows, though, maybe you really like the smell of mustard...

Ed Altorfer
I do like mustard...
James
Fair enough. You're allowed to write all of your error handling code after the fact. :)
Ed Altorfer