This would be a perfectly valid thing to do, except it would be better if you had specific exception sublcasses, such as "MissingIdException" and "IdNotFoundException" or similar. The idea to ensure that the class name, rather than the message, differentiates the exceptions so the code down the call stack can correctly decide how to handle any caught exception.
Regards,
Bryce Lohr
David Mintz wrote:
On Sat, Jul 19, 2008 at 1:35 AM, Adam Jensen <jazzslider@gmail.com> wrote:
<snip/>OK...so if the object can't be constructed valid, it shouldn't be
constructed at all. As far as I know, the only way to do that in PHP
is with exceptions, as per my first example. And then I can use
multiple exception subclasses to handle the various reasons why the
construction failed, which the controller can use to provide the user
with appropriate error messages.
So -- would you advocate, e.g., a model constructor roughly like
function __construct($id = null) {
if (! $id) { throw new Exception("missing id parameter"); }
if (!$this->isValidId($id)) {throw new Exception("invalid id"); }
// instantiate a subclass of Zend_Db_Table_Abstract $this->someTable
if (! $object = $this->someTable->find($id)) {
throw new Exception("record $id not found"); }
// continue initializing etc
}
I seem to recall reading somewhere that throwing exceptions in a constructor is bad form but I forget where or why.
--
David Mintz
http://davidmintz.org/
The subtle source is clear and bright
The tributary streams flow through the darkness
没有评论:
发表评论