As the title says, I wonder if I should handle errors for :
Can it actually fail to set?
And if it can, what could be a reason for it to fail?
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It only takes a minute to sign up.Sign up to join this community
As a general rule, if some function of some API is documented (explicitly or implicitly) as being capable of failure, you should account for that failure.
This means you check for the failure directly (checking the return value in the case of
SDL_GL_SetAttribute) or you ensure earlier in your code that the conditions leading to failure cannot be true by the time you call the function. Even if you're doing the second option, it may be useful to check the return code directly in a way that's compiled out of release builds, for debugging or logging purposes.
The reason is because an API represents a contract between you and the implementer of that API, and part of that contract includes error checking. A well-written API should be easy to use correctly, and that means if errors are documented as possible, the author probably had a good reason to do so.
Of course, it is possible for APIs to be poorly written, and have documentation that they can fail that is incorrect; but it's also possible that they just can't fail today and might be updated to fail in the future and the author is being future-proof, so... it's a toss up. Better for you to write your code defensively.
Specifically in the case of
SDL_GL_SetAttribute, you can see that it can fail in a variety of ways (as of the writing of this answer):
valueparameter is not a legal value for that attribute.