I have a class:
class Vector {
public:
element* get(int i);
private:
element* getIfExists(int i):
};
get invokes getIfExists; if element exists, it is returned, if not, some action is performed. getIfExists can signal that some element i is not present
either by throwing exception, or by returning NULL.
Question: would there be any difference in performance? In one case, get will need to check ==NULL, in another try... catch.
Its a matter of design, not performance. If its an exceptional situation -like in your
getfunction- then throw an exception; or even better fire an assert since violation of a function precondition is a programming error. If its an expected case -like in yourgetIfExistfunction- then don’t throw an exception.Regarding performance, zero cost exception implementations exist (although not all compilers use that strategy). This means that the overhead is only paid when an exception its thrown, which should be… well… exceptionally.