views:

93

answers:

5

What error checking do you do? What error checking is actually necessary? Do we really need to check if a file has saved successfully? Shouldn't it always work if it's tested and works ok from day one?

I find myself error checking for every little thing, and most of the time if feels overkill. Things like checking to see if a file has been written to a file system successfully, checking to see if a database statement failed.......shouldn't these be things that either work or don't?

How much error checking do you do? Are there elements of error checking that you leave out because you trust that it'll just work?

I'm sure I remember reading somewhere something along the lines of "don't test for things that'll never really happen".....can't remember the source though.

So should everything that could possibly fail be checked for failure? Or should we just trust those simpler operations? For example, if we can open a file, should we check to see if reading each line failed or not? Perhaps it depends on the context within the application or the application itself.

It'd be interesting to hear what others do.

UPDATE: As a quick example. I save an object that represents an image in a gallery. I then save the image to disc. If the saving of the file fails I'll have to image to display even though the object thinks there is an image. I could check for failure of the the image saving to disc and then delete the object, or alternatively wrap the image save in a transaction (unit of work) - but that can get expensive when using a db engine that uses table locking.

Thanks,

James.

A: 

I generally follow these rules.

  1. Excessively validate user input.
  2. Validate public APIs.
  3. Use Asserts that get compiled out of production code for everything else.
ChaosPandion
A: 

if you run out of free space and try to write file and don't check errors your appliation will fall silently or with stupid messages. i hate when i see this in other apps.

Andrey
maybe you should buy a bigger hard drive?
Malfist
very funny. it was just an example. one more is lack of rights to write to certain dir.
Andrey
+1  A: 

I'm not addressing the entire question, just this part:

So should everything that could possibly fail be checked for failure? Or should we just trust those simpler operations?

It seems to me that error checking is most important when the NEXT step matters. If failure to open a file will allow error messages to get permanently lost, then that is a problem. If the application will simply die and give the user an error, then I would consider that a different kind of problem. But silently dying, or silently hanging, is a problem that you should really do your best to code against. So whether something is a "simple operation" or not is irrelevant to me; it depends on what happens next, or what would be the result if it failed.

MJB
A: 

Regarding your example...

I save an object that represents an image in a gallery. I then save the image to disc. If the saving of the file fails I'll have [no] image to display even though the object thinks there is an image. I could check for failure of the the image saving to disc and then delete the object, or alternatively wrap the image save in a transaction (unit of work) - but that can get expensive when using a db engine that uses table locking.

In this case, I would recommend saving the image to disk first before saving the object. That way, if the image can't be saved, you don't have to try to roll back the gallery. In general, dependencies should get written to disk (or put in a database) first.

As for error checking... check for errors that make sense. If fopen() gives you a file ID and you don't get an error, then you don't generally need to check for fclose() on that file ID returning "invalid file ID". If, however, file opening and closing are disjoint tasks, it might be a good idea to check for that error.

Mike DeSimone
A: 

This may not be the answer you are looking for, but there is only ever a 'right' answer when looked at in the full context of what you're trying to do.

If you're writing a prototype for internal use and if you get the odd error, it doens't matter, then you're wasting time and company money by adding in the extra checking.

On the other hand, if you're writing production software for air traffic control, then the extra time to handle every conceivable error may be well spent.

I see it as a trade off - extra time spent writing the error code versus the benefits of having handled that error if and when it occurs. Religiously handling every error is not necessary optimal IMO.

Jeremy