Re: Throw Exceptions from setters: second opinion
amygdala wrote:
Hi all,
Recently somebody on a forum told me, and nearly convinced me, that
throwing Exceptions from setters is bad practice. He stated that setters
should only return boolean false if the provided input is not valid,
since that is what they are expected to do: validate input and simply
tell wheter it's valid or not. Furthermore he stated that throwing
Exceptions is only meaningful when an internal routine of the class
encounters invalid data. Does this sound reasonable to you? I'm not sure
yet myself. Love to hear your opinions on the matter.
Thanks in advace.
Not at all. It's actually quite a good idea to throw exceptions from
setters.
Returning true/false from a setter is old school - from before the time
exceptions existed. Nowadays, a better way is to return the value you
are setting it to. That way you can chain operations.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@xxxxxxxxxxxxx
==================
.
Relevant Pages
- Throw Exceptions from setters: second opinion
... He stated that setters should only return boolean false if the provided input is not valid, since that is what they are expected to do: validate input and simply tell wheter it's valid or not. ... Furthermore he stated that throwing Exceptions is only meaningful when an internal routine of the class encounters invalid data. ... (comp.lang.php) - Re: Throw Exceptions from setters: second opinion
... He stated that setters should only return boolean false if the provided input is not valid, since that is what they are expected to do: validate input and simply tell wheter it's valid or not. ... Furthermore he stated that throwing Exceptions is only meaningful when an internal routine of the class encounters invalid data. ... // act on return value false of tellMyObjectToTakeAction ... (comp.lang.php) - Re: Throw Exceptions from setters: second opinion
... He stated that setters should only return boolean false if the provided input is not valid, since that is what they are expected to do: validate input and simply tell wheter it's valid or not. ... Furthermore he stated that throwing Exceptions is only meaningful when an internal routine of the class encounters invalid data. ... That way you can chain operations. ... // act on return value false of tellMyObjectToTakeAction ... (comp.lang.php) - Re: Throw Exceptions from setters: second opinion
... He stated that setters should only return boolean false if the provided input is not valid, since that is what they are expected to do: validate input and simply tell wheter it's valid or not. ... Furthermore he stated that throwing Exceptions is only meaningful when an internal routine of the class encounters invalid data. ... // act on return value false of tellMyObjectToTakeAction ... (comp.lang.php) - Re: Throw Exceptions from setters: second opinion
... He stated that setters should only return boolean false if the provided input is not valid, since that is what they are expected to do: validate input and simply tell wheter it's valid or not. ... Furthermore he stated that throwing Exceptions is only meaningful when an internal routine of the class encounters invalid data. ... // act on return value false of tellMyObjectToTakeAction ... Because that is how I've seen it being called in stead of chained operations. ... (comp.lang.php) |
|