开发者

What's a good approach to writing error handling?

开发者 https://www.devze.com 2022-12-15 08:21 出处:网络
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\'

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 wi开发者_如何学编程th database connection failures and potential effects on the front end, etc. Keep them coming!


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.


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.


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.


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.


I have two words for you: Defensive Coding


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.


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...

0

精彩评论

暂无评论...
验证码 换一张
取 消