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?
Basically, there are two steps: First, allocate raw memory for the object. Second, call the constructor in that memory, creating the object. If the second step throws an exception, enter stack unwinding. Else schedule the destructor call.
Yes, constructors are allowed to throw an exception in Java. A Constructor is a special type of a method that is used to initialize the object and it is used to create an object of a class using the new keyword, where an object is also known as an Instance of a class.
The short answer to the question “can a constructor throw an exception in Java” is yes! Of course, properly implementing exceptions in your constructors is essential to getting the best results and optimizing your code.
Just use exceptions throughout. Error return values have all manner of problems, particularly they are too easily ignored. Yes, constructor should either construct a valid object or throw an exception. @Antoine That is a horrible idea, and has repeatedly been shown to have many, many problems, Just use exceptions.
A good method is not to allow the error to compile!
Unfortunately, merely using unsigned
will not prevent a negative value from being passed to the constructor – it will just be converted implicitly to an unsigned
value, 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:
struct nonnegative {
nonnegative() = default;
template <typename T,
typename = typename std::enable_if<std::is_unsigned<T>::value>::type>
nonnegative(T value) : value{value} {}
operator unsigned () const { return value; }
private:
unsigned value;
};
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 signed
to unsigned
numbers.
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 option
type or something similar).
Often you would encapsulate this throwing of an exception in some kind of ASSERT
macro. 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:
unsigned value;
if (! (std::cin >> value))
// handle user input error, e.g. throw an exception.
No. Throw an exception — that is the standard way to bail out from the ctor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With