3 ms·
A common pattern for dealing with failures on construction, such as the example above, without using exceptions is for the object to be constructed in a null st
by jandrewrogers 9y ago
A common pattern for dealing with failures on construction, such as the example above, without using exceptions is for the object to be constructed in a null state (often useful in any case) and implementing a 'explicit operator bool()' overload that evaluates to false if construction failed or is intentionally null. Of course, that means you have to actually check that construction was successful, similar to returning an error code. It doesn't require you to give up copy constructors etc if they are sensible (and these are usually, but not always, best served by move constructors).
- rramdin 9y agoBut how is requiring the programmer to know that he has to check error codes and whatnot any different from requiring the call be place in a try/catch block? Most cases where exceptions are useful are when the alternative is knowingly causing such a state that will inevitably lead to a seg fault. It's much easier to raise at the point of failure instead of trying to limp until the application eventually crashes or loses you all your money.
- erik_seaberg 9y agoYour code isn't really statically typed if every object might support its declared methods, or might instead be an unusable time-bomb. Much better that an invalid object never exists and code that needs that object isn't reachable.