There is such class:
class Circle{
int x;
int y;
int radius;
public:
Circle(int x_, int y_, int radius_){
x = x_;
y = y_;
if(radius < 0)
signal error;
radius = radius_;
}
};
It is not possible to create circle with radius less than zero. What is a good method to signal error in constructor? I know that there are exceptions, but are there any other methods?
A good method is not to allow the error to compile!
Unfortunately, merely using
unsignedwill not prevent a negative value from being passed to the constructor – it will just be converted implicitly to anunsignedvalue, thus hiding the error (as Alf pointed out in a comment).The “proper” solution would be to write a custom
nonnegative(or more generally,constrained_value) type to make the compiler catch this error – but for some projects this may be too much overhead, since it can easily lead to a proliferation of types. Since it improves (compile-time) type safety, I still think that this is fundamentally the right approach.A simple implementation of such a constrained type would look as follows:
See it in action
This prevents construction from anything but an unsigned type. In other words, it simply disables the usual implicit, lossy conversion from
signedtounsignednumbers.If the error cannot be caught at compile time (because the value is received from the user), something like exceptions are the next best solution in most cases (although an alternative is to use an
optiontype or something similar).Often you would encapsulate this throwing of an exception in some kind of
ASSERTmacro. In any case, user input validation should not happen inside a class’ constructor but immediately after the value has been read from the outside world. In C++, the most common strategy is to rely on formatted input: