Error checking overkill?

What error checking are you doing? What error checking is really necessary? Do we really need to check if the file was saved successfully? Doesn't it always work if it is tested and works fine from day one?

I found an error in checking every little thing, and most of the time if I feel an excessive reaction. Things like checking to check if the file was written to the file system successfully, checking if the database instruction worked ... should this work or not?

How many error checks do you make? Are there error checking elements that you do not account for because you trust that it will work?

I am sure that I remember somewhere reading something like the lines โ€œdo not check things that will never happenโ€ ..... I canโ€™t remember the source.

So, should everything that might be possible be tested for failure? Or should we just trust these simpler operations? For example, if we can open a file, we need to check if each line was read or not? Perhaps this depends on the context within the application or the application itself.

It would be interesting to hear what others are doing.

UPDATE: As a quick example. I save an object representing the image in the gallery. Then I save the image to disk. If saving the file fails, I will have to display the image, even if the object believes that there is an image. I can check if it was not possible to save the image to disk, and then delete the object or, conversely, wrap the image in a transaction (unit of work), but this can become expensive when using the db engine using table locking.

Thanks,

James.

+6
error-checking
source share
5 answers

if you run out of free space and try to write the file, and do not check for errors, your applications will crash silently or with stupid messages. I hate it when I see it in other applications.

+1
source share

I am not considering the whole question, just this part:

So should everything that could possibly be impossible to verify for failure? Or do we just have to trust the more simple operation?

It seems to me that error checking is most important when the NEXT step matters. If a failure to open the file allows error messages to get lost, this is the problem. If the application just dies and gives the user an error, I would think that this is another problem. But silence or silence is a problem that you should do your best to make code against. Whether something is a "simple operation" or not, it does not matter to me. it depends on what happens next, or what will be the result if it fails.

+1
source share

I usually follow these rules.

  • Excessive user input validation.
  • Validate public APIs.
  • Use Asserts that compile from production code for everything else.
0
source share

Regarding your example ...

I save an object representing the image in the gallery. Then I save the image to disk. If the file cannot be saved, I will see a [no] image, even if the object believes that there is an image. I can check if it was not possible to save the image to disk, and then delete the object or, conversely, wrap the image in a transaction (unit of work), but this can become expensive when using the db engine using table locking.

In this case, I would recommend first saving the image to disk before saving the object. Thus, if the image cannot be saved, you do not need to try to cancel the gallery. In the general case, dependencies must first be written to disk (or placed in a database).

Regarding error checking ... check for errors that make sense. If fopen() gives you the file identifier and you do not get an error, you usually do not need to check for fclose() on this file identifier that returns an "invalid file identifier". If, however, opening and closing files are tasks associated with decoupling, it might be a good idea to check for this error.

0
source share

This may not be the answer you are looking for, but there is always the โ€œrightโ€ answer when you look in the full context of what you are trying to do.

If you write a prototype for internal use, and if you get an odd error, it does not matter, then you spend time and money in the company, adding additional verification.

On the other hand, if you are writing industrial air traffic control software, then the extra time it takes to process any conceivable error can be well spent.

I see this as a compromise โ€” the extra time it takes to create the error code, and the benefits of handling this error if and when it happens. Religious treatment of each error is not optimal for IMO.

0
source share

All Articles