Exceptions or Error Return Codes?

In the initial version of my WinReg C++ library, I made a design decision that methods that wrapped Win32 Registry C API calls would throw C++ exceptions on errors.

Important feedback I got from the users of this library was that they wanted methods that returned error codes instead of throwing exceptions (e.g., see this issue).

For example, when opening a registry key, folks want to be able to check the result of the operation without having exceptions thrown from failed opens. Similarly when trying to read values from the registry.

This is a good remainder for those C++ devs who think that we should always throw exceptions instead of using error return codes!


2 Replies to “Exceptions or Error Return Codes?”

  1. I prefer error codes to exceptions in most cases but a case can be made for a library like WinReg to support both, using either a mode or versions of methods for both.

    1. Thanks for your comment. In fact, I do support both in WinReg, e.g. the initial exception-throwing methods like DWORD RegKey::GetDwordValue(), or RegKey::GetStringValue(), etc., and the new error-returning TryGetXxx() methods.

Leave a Reply

Your email address will not be published. Required fields are marked *