Is it legitimate to have exception handling code in a class constructor, or should it be avoided? Should one avoid having exception-generating code in a constructor?
Is it legitimate to have exception handling code in a class constructor, or should
Share
Yes, it’s perfectly reasonable. How else would you have a circumstance like this:
A
Listwith negative length is most certainly exceptional. You can not let this get back to the caller and have them think that construction was successful. What’s the alternative, aCheckIfConstructionSucceededinstance member function? Yucky.Or what about
Again, this is a throw and nothing else is acceptable.