Could an object refuse to be created!?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello

I was wondering is it possible to refuse the creation of an object. For example if the user doesn’t enter a correct string (as a parameter in the constructor), could the object refuse to be created!
 
Sam said:
I was wondering is it possible to refuse the creation of an object. For example if the user doesn’t enter a correct string (as a parameter in the constructor), could the object refuse to be created!?

Yes, throw an Exception in the object's constructor. This can be
somewhat ugly to deal with depending on how the object's user is trying
to instantiate the object, but it will definitely keep the object from
being created.
 
Kevin said:
Yes, throw an Exception in the object's constructor. This can be
somewhat ugly to deal with depending on how the object's user is trying
to instantiate the object, but it will definitely keep the object from
being created.

Actually, it is not ugly. This approach is used all over the .net.

Sunny
 
Sunny said:
Kevin P. Fleming wrote:




Actually, it is not ugly. This approach is used all over the .net.

Oh, I know. I was trying to make sure the OP knew that things like this:

private void Foo()
{
SomeObjectType obj = new SomeObjectType();
}

can be a pain to deal with if SomeObjectType's constructor throws an
exception. It's even worse if these initializations are at the type
level, and if they are for static fields, then they could happen any
time at all and be _very_ confusing.

That doesn't it's not the right way to do it though; given a choice
between the constructor throwing an exception and having the object be
in "limbo" until it's initialized I'd take the former every time :-)
 
Hi Kevin, I know what you mean. My idea was that OP has to read the
documentation :)

During all these years thats the only think I have learned :)
And of course, I have started with: "If nothing else helps, read the
docs" :)

Sunny
 
Would be nice to note, that in cases where your object creation is linked
to valid input criterion, that you might instead use a factory method. Factory
methods demonstrate, better than constructors, that work is going to occur
and that validation of parameters might be necessary. Factory methods are
also capable of returning null rather than throwing an exception. To then
prevent the user from instantiating your type directly, you simply privatize
the constructor.
 
Justin said:
Would be nice to note, that in cases where your object creation is linked
to valid input criterion, that you might instead use a factory method. Factory
methods demonstrate, better than constructors, that work is going to occur
and that validation of parameters might be necessary. Factory methods are
also capable of returning null rather than throwing an exception. To then
prevent the user from instantiating your type directly, you simply privatize
the constructor.

Very good point. A simple static factory method on the same type would
suffice.
 
Justin Rogers said:
<snipped>
To then
prevent the user from instantiating your type directly, you simply
privatize
the constructor.

Umm, not quite... To _discourage_ users form instantiating directly, you
simply privatize the constructor. There's nothing particularly simple about
_preventing_ users form instantiating your type.
 
Back
Top