Error codes and asserts
Error codes are a common way of reporting and handling errors in C. They are also still used in C++. A function that fails reports an error through enumerated codes that are checked by a caller and handled appropriately. Let us analyze how error codes work from both the caller and the callee perspective.
A function that returns an error must have a list of errors that are exposed to callers. This list is maintained through a software life cycle, and it can be subject to changes. Enumerated error codes can be added, removed, or modified. A caller must be aware of the error codes that the callee is returning, and it needs to handle them. Or, if it doesn’t know how to handle an error, it should propagate it further within a call stack.
Let’s observe a simple example of a function that returns an error and analyze the implications this has for the code using this function:
enum class error {
Ok,
Error1,
Error2,
Unknown...