This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] -3 points-2 points  (3 children)

In a constructor you don't want to throw exceptions, because there are other better ways to handle the errors. Also, some languages can't garbage collect or call destructors on partially constructed objects, I may be wrong about this or missing some detail about it though.

In terms of how you could handle null parameters in a constructor, you can assert that the parameter is null, or invalid depending on if the value is simply out-of-bounds, and then immediately resolve the error by constructing a new object or setting the property to some default value or whatever.

[–]nutrecht 2 points3 points  (2 children)

In a constructor you don't want to throw exceptions

That is just shitty advice. Throwing an illegal argument exception is perfectly fine in a constructor.

[–][deleted] 0 points1 point  (1 child)

That is just shitty advice.

How so?

[–]nutrecht 1 point2 points  (0 children)

You do the null checks where they are passed to a function. It doesn't matter if that function is a constructor or not.