Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error checking overkill?

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.

like image 736
James Avatar asked Apr 05 '10 18:04

James


2 Answers

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.

like image 107
Andrey Avatar answered Oct 25 '22 14:10

Andrey


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.

like image 20
MJB Avatar answered Oct 25 '22 14:10

MJB