Professional PHP

PHP Programming, Web Development, PHP Advocacy and PHP Best Practices.
« Design Eye for the Usability Guy
Upgraded to WordPress 1.2 »

Exceptional PHP

May 24th, 2004

With PHP 5 adding exceptions, I imagine that the PHP community will have to undergo a bit of a paradigm change regarding PHP error handling. This transition probably won't be without controversy and heartache, especially for those integrating third party libraries. I suspect that some of these issues won't get worked out until 5.1.

Wez Furlong has a proposal for structured errors in PHP. I like the idea of standard error codes. I don't much care for the exception_map or the superglobal part, though.

Even languages that have had exceptions forever still have controversy over the issue. Joel on Software considers exceptions harmful and favors status returns:

A better alternative is to have your functions return error values when things go wrong, and to deal with these explicitly, no matter how verbose it might be. It is true that what should be a simple 3 line program often blossoms to 48 lines when you put in good error checking, but that's life, and papering it over with exceptions does not make your program more robust.

Doug Ross agrees with Joel and talks a bit about poor software quality. It almost seems like he is elevating exceptions considered harmful into exceptions kill:

In other words, many of us are undisciplined. We're more worried about "readability" (and I disagree with that contention as well - but I'll get to that) than whether or not or software will kill anyone, debit the wrong account by a million bucks, or screw up the actuarial table for 83 year-old transvestites.

I agree with Ned Batchelder's comprehensive rebuttal in exceptions vs status returns. Bob Balaban comments on this with an anecdote:

Years ago at Lotus one of the architects did an actual benchmark of some code in 123 written first with the status technique and then with the exception technique, and claimed that the exception technique was 15% less code (he was comparing compiled code size) and maybe 5% faster.

Following my API Design principles, I think the smallest amount of code wins. If exceptions can reduce the amount of code, then that is the best way to go. After all, error propagation code can have errors too. Code verbosity considered harmful.

categories PHP, Software Design
tags exceptions, php

Related Posts

  • php|tek Slides
  • Zend Framework Webcast
  • goto in PHP
You can leave a response, or trackback from your own site.

11 Responses to “Exceptional PHP”

  1. #1 Harry Fuecks responds...
    May 25th, 2004 at 12:04 am

    One thing that bothers me on the subject of exceptions in PHP is I've yet to see anyone in PEAR approach the subject of a standard exceptions. Most languages using exceptions (including Java, Python and Javascript) provide a standard set of exception classes for common issues such as these. This is a great opportunity for PEAR to make itself both invaluable to PHP and right some of the error handling wrongs it's done in the past.

    Think the big plus of exceptions is it provides a standard mechanism everyone can use - the big issue with PHP4 is everyone's got a different approach and glueing them together, when using different libraries, is often painful. Some people are saying exceptions are slower compared to other flow control structures - haven't confirmed this for myself but my guess is while it may be true at first glance, it's not so true when writing real code e.g.


    $result = someFunc();

    switch ( $result ) {
    case ERROR_X:
    // Do something
    break;
    case ERROR_Y:
    // Do something
    break;
    case ERROR_Z:
    // Do something
    break;
    default:
    // Do the stuff that happens when there are no errors
    break;
    }

    The cost of evaluating each condition in the switch is incurred every time it's used while the alternative;


    try {
    $result = someFunc();
    // Do the stuff that happens when there are no errors
    } catch (Error_X e) {

    } catch (Error_Y e) {

    } catch (Error_Z e) {

    }

    Leaves evaluation of the type of error to only those cases where an error actually occurs (which is hopefully rare).

    Personally what I'd like to see is the possibility of being able to trap old style errors in a try catch block (but if you don't use one, the error propogates to the traditional global error handler). Would allow for code like;


    try {
    $result = eval($someCode);
    } catch (e) {

    }

    Javascript does a very good job of this - you can even hard code syntax errors and catch them as exceptions. That's not to say this is the way applications should be designed but there are circumstances where it's useful and it implies a well implemented exception mechanism. I get the feeling we're going to end up with a mess in PHP between the old mechanism and the new exceptions.

    Would be interesting to discuss error handling strategy in "userland" PHP. Think in helps to consider what type of error we're having to deal with. Based on the definitions I came up with here some random thoughts;

    - Syntax errors: use the old mechanism (programmers will want to fix these immediately) but allow exceptions for special cases like eval() and create_function()

    - Semantic Errors: should mainly use the old mechanism (some will be a programmers mistake and something they want to fix) but perhaps exceptions for usage errors (e.g. divide by zero). At the same time if proper validation is being performed, the zero should never get to the function that might divide by it.

    - Environment errors: this is where exceptions have something significant to error and should be the mechanism of choice, at least for libraries. Can't connect to the database, open a file or whatever - throw an exception.

    A forth type of error might be "User Errors" but these are question of validation IMO.

  2. Smoking toooo much PHP trackbacked on June 17th, 2004 at 5:30 pm
  3. Smoking toooo much PHP trackbacked on June 17th, 2004 at 5:32 pm
  4. Smoking toooo much PHP trackbacked on June 17th, 2004 at 6:14 pm
  5. Smoking toooo much PHP trackbacked on June 17th, 2004 at 6:14 pm
  6. Smoking toooo much PHP trackbacked on June 17th, 2004 at 6:27 pm
  7. #7 Lukas responds...
    June 18th, 2004 at 2:56 am

    Harry I really like your definition. I agree that exceptions make perfect sense for enviornment errors but not for syntactical or semantical errors. User errors shouldnt be exceptions as they incorrect input should be expected behaviour and if any they would be syntactical mistakes.

  8. cavorite trackbacked on June 20th, 2004 at 6:02 am
  9. Jeff Moore's Blog » goto in PHP pingbacked on July 29th, 2004 at 7:10 pm
  10. #10 Anonymous responds...
    August 26th, 2005 at 1:37 am

    Could you please send to me the contacts of developer of your site? It looks so damn good!

  11. Zend Framework Webcast | Professional PHP pingbacked on December 5th, 2005 at 1:45 pm

Leave a Reply

XHTML: You can use these tags: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <div align=""> <em> <font color="" size="" face=""> <i> <li> <ol> <strike> <strong> <sub> <sup> <ul>

code: use [code=php][/code].

Comment Preview

  • Search

  • Subscribe

    Subscribe All Posts
    Subscribe All Comments
    Subscribe All Bookmarks
    Subscribe with Bloglines Subscribe with My Yahoo Add to netvibes Subscribe in NewsGator Online Subscribe with Google feed reader
  • Share This

  • Categories (Home)

    • Agile Methods (14)
    • Mac (14)
    • Misc (16)
    • Open Source (14)
    • PHP (93)
    • Software Design (27)
    • Usability (14)
    • WACT (7)
    • Web Design (20)
  • Recent Comments

    • Sarah Snow Stever  23
      Snowcore, ennah, Philippine Website Developers [...]
    • PHP Development From Java Architects Eye  9
      Bobrila, FelhoBacsi, Angsuman Chakraborty [...]
    • Working with PHP 5 in Mac OS X 10.5 (Leopard)  83
      hzpevghnte, Luis Oscar Cruz, xentek [...]
    • Improved Error Messages in PHP 5  9
      ennah, Khumaer, retry [...]
    • The value of MVC  9
      Vulchak, อะไหล่แอร์, Alyson Serrano [...]
    • Why PHP is easier to learn than Java  13
      , , WTF [...]
    • Yahoo YUI wins JavaScript Library Wars  9
      cfkjdiqovw, Jeff, Patrick Mueller [...]
    • goto in PHP  38
      Goldilocks, , SFM [...]
    • Decline of Google  3
      Dallas Graham, Will Mcclure, Harry Fuecks
    • Mouse problems with Safari 1.3 after using Expose  1
      Leigh Townsend
    • Design Eye for the Usability Guy  1
      Holli Holden
  • Pages

    • Tags
  • Recent Posts

    • Sarah Snow Stever
    • Benchmarking PHP’s Magic Methods
    • The Endpoints of the Scale of Stupidity on Video
    • Working with PHP 5 in Mac OS X 10.5 (Leopard)
    • Keywords and Language Simplicity
    • Improved Error Messages in PHP 5
    • Michigan Taxes Graphic Design Services
    • Ruby versus PHP or There and Back Again
    • Mighty Mouse Kryptonite and Exceeding Expectations
    • reCAPTCHA - Combining Distributed Problem Solving with a Web Service
  • Archives

    • 2007: Jan Feb Mar Apr May Sep Oct Nov
    • 2006: Jan Feb Mar Apr May Jun Jul Oct Nov Dec
    • 2005: Jan Feb Mar Apr May Sep Oct Nov Dec
    • 2004: Apr May Jun Jul Aug Sep Oct Nov
  • Menu

    • Register
    • Login