this is the life :)

From: Date: Sat, 10 Jul 2004 07:48:51 +0000
Subject: this is the life :)
Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-31819@lists.php.net to get a copy of this message
Hello all, Returned from our successful concert in Massachusetts at 2 AM, read email, posted a large new section on design issues to think about with exceptions on the wiki. One easy way to summarize the mistakes I see in espousing exceptions: when looking for a better way to do things, design for the worst possible use, not the best. PEAR_Exception is an excellent example of designing for the worst possible use. Exceptions at their worst are completely internal, do not even notify outer levels that anything wrong has happened, leap all over the place, and are generally impossible to debug. PEAR_Exception can actually help correct a series of spaghetti code decisions through observers. In other words, it is possible to look at the PEAR_Exception hierarchy in your code as it is written (throw within try/catch), or as a flat hierarchy of sequential exceptions as seen by observers - and even caught exceptions are seen. In other words, contrary to popular belief, exceptions do not force you to handle an error. <?php try {
    throw new Exception('see?');
} catch (Exception $e) { } echo 'hello'; ?> Surely, no one but an idiot would write the above code (grin), but the point is PHP will not display any error message whatsoever if you do happen to write this. A more realistic example: <?php $t = false; ... try {
    $tt = $obj->doSomething();
    $another = $obj->doFollowup();
} catch (Exception $e) {
    if ($t) {
        // handle the exception
    }
} echo 'hello'; ?> This is still bad design since there should be 2 try/catch blocks in this case, but it looks better. Inexperienced users WILL write code like the above, and it will most likely work in most cases. However, the 1 typo ($tt = ...) may not be caught for a long time. PEAR_Exception allows users (to a certain extent) to actually find this bug by setting an observer that prints out every created exception, and then noticing that an exception is being raised in doFollowup(), but is not being properly handled in the catch clause. It doesn't improve the design, but does improve the resulting code. This must be the goal of any error handling system. Help find and remove errors in both the code and in the user's input. The only way to do this is to provide flexibility in error severity as well as flow control. throw provides excellent flow control flexibility, but absolutely no severity flexibility beyond "catch this or die." The exception class, on the other hand, through its use of OO, is very flexible, and does provide a great way to implement flexible error severity. I whipped up a proof-of-concept class that just might solve the problems ErrorStack (now PEAR_ErrorStack) was built to solve, and it is compatible with PEAR_Exception. This means there is a possibility of converting a warning directly into a standard exception by simply throwing it. I leave again on Sunday, but in the mean time, let's have some fun. :) Greg P.S. I also converted PEAR_ErrorStack into E_STRICT to see how hard it was, and could commit it as PEAR_ErrorStack5.php if there is interest. The API is identical, only the internals have changed, so the classname remains PEAR_ErrorStack.

« previous php.pear.dev (#31819) next »