Is it possible to stop an object from being created during construction?
5066 Since 25th November, 2003
Select and Copy the Code
Yes, have the constructor throw an exception. Formally, an object _will_ be created (since the constructor is a method invoked after the actual method creation), but nothing useful will be returned to the program, and the dead object will be later reclaimed by Garbage Collector.
But the clean way is that you leave calls to the constructor to a static factory method which can check the parameters and return null when needed.
Note that a constructor - or any method in general - throwing an exception will not "return null", but will leave the "assign target" as it was.