I have a library which basically stores a collection of objects, each of which has a unique id.
The library has certain functions which, for example, return a variable in relation to a given object. The argument for such a function would be a unique id.
But what should happen when an object with that id does not exist? Clearly, I cannot return a 'false' bool when the function would return a string if the object does exist. Should I throw an exception here?
Depending on the type of data passed back, be it a pointer, an int etc you can return a invalid value such as nullptr , 0 or -1, then test for such when calling the function such as
Unless you use a parameter to pass the output variable and use the return mechanism to return a status indicator there is really no one elegant way to handle errors other than throwing and catching an exception.
No matter how you intend to solve the problem you should insure that your function's documentation fully describes what happens if the pre or post conditions (and what those pre and post conditions actually are) are not satisfied.