Should programs check for a failure on WinAPI functions that "shouldn't" but can fail?

I recently updated some code used to take screenshots using the WinAPI feature set in GetWindowDC β†’ CreateCompatibleDC β†’ CreateCompatibleBitmap β†’ SelectObject β†’ BitBlt β†’ GetDIBits. Now I check all these errors because they can and sometimes fail. But then I have to clean up by deleting the created bitmap, deleting the created dc and letting go of the dc window. In any example that I saw - even on MSDN - related functions (DeleteObject, DeleteDC <ReleaseDC) are not checked for failure, apparently because if they were received / created by OK, they will always be deleted / released by OK. But they can still fail.

This is just one noteworthy example, since the challenges are next to each other. But sometimes there are other functions that can fail, but in practice they never do. For example, GetCursorPos. Or functions that may fail only if invalid data is transferred, such as FileTimeToSytemTime.

So, is it good to check ALL functions that may fail to fail? Or is there anything in order not to check? And as a result, when checking these functions "never-unsuccessfully" for failure, what is right? Throw an exception at runtime using assert, something else?

+7
source share
6 answers

Yes. You never know when the promised service will surprise you without working. Better to report a bug even for surprises. Otherwise, you will find yourself with the client, saying that your application does not work, and the reason will be a complete secret; you will not be able to respond in a timely manner, useful to your client, and both of you will lose.

If you organize your code to always do such checks, it is not so difficult to add the following check to the next API that you are calling.

+4
source

The question of whether to test or not depends on what you would do if this did not work. Most samples exit after cleaning is completed, so checking for proper cleaning does not serve the purpose; the program exits anyway.

Not checking something like GetCursorPos can lead to errors, but depending on the code needed to fix it, whether you should check or not. If you check this, add 3 lines around all your calls, then you will most likely be better off taking a chance. However, if you have a macro setup for processing, then it doesn’t hurt to add this macro just in case.

FileTimeToSystemTime checked, depending on what you are going into it. File time from system? It is probably safe to ignore it. Custom string built from user input? probably better to make sure.

+5
source

It's funny that you mention GetCursorPos, as this does not work on Wow64 processes when the address has passed> 2Gb. He fails every time. The error was fixed in Windows 7.

So yes, I think it is wise to check for errors, even if you do not expect them.

+2
source

Yes. Suppose you do not check which function has returned, and the program simply continues after the function fails. What will happen next? How do you know why your program has been malfunctioning for a long time?

One fairly reliable solution is to throw an exception, but for this, your code will be safe for exceptions.

+1
source

Yes. If a function can fail, you must protect it.

One useful way to categorize potential problems in your code are the potential causes of the failure:

  • Invalid Code Operations
  • invalid operations in client code (code that calls yours, written by someone else)
  • external dependencies (file system, network connection, etc.).

In situation 1, it is enough to detect an error and not perform recovery, as this is an error that must be corrected by you.

In situation 2, the error should be reported to the client (for example, by throwing an exception).

In situation 3, your code should be restored as automatically as possible, and if necessary, specify the client code.

In both situations 2 and 3, you should try to make sure that your code is restored to an acceptable state, for example. you should try to suggest a "strong exception guarentee" etc.

+1
source

Yes, you need to check, but if you use C ++, you can use RAII and leave cleanup for the various resources that you use.

An alternative would be to have a mess of if-else statements, and it is really ugly and error prone.

+1
source

All Articles