1
\$\begingroup\$

As the title says, I wonder if I should handle errors for :

SDL_GL_SetAttribute

Can it actually fail to set?

And if it can, what could be a reason for it to fail?

\$\endgroup\$
2
\$\begingroup\$

Probably.

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):

  • It will fail if the video subsystem isn't initialized.
  • It will fail if the value of the attribute parameter is invalid.
  • It will fail if the value of the attribute parameter is SDL_GL_CONTEXT_FLAGS or SDL_GL_CONTEXT_PROFILE_MASK and the value parameter is not a legal value for that attribute.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.