SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2004
    Location
    naperville
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    When is it appropriate to use exceptions?

    I have some small Java background (read - I'm literate in reading it and took a uni class in it), but I'm still not sure when exceptions are appropriate. If everything is being validated, should you throw them when a rule isn't met? When you open a file?

    When do you use exceptions (vs normal error handling, if any).

    In fact, I'd like to expand the question to, "How do you handle errors in PHP, both userbased and otherwise, with an emphasis on exceptions?"

  2. #2
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is another, very hot thread on this very topic. My position is that yes, you should use them in most cases you mention in the first paragraph. Some seem to disagree, though.

  3. #3
    SitePoint Zealot
    Join Date
    Oct 2004
    Location
    naperville
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I saw that, and thats why this came up. I didn't want to totally hijack it though ... it seemed to be going back to the GOTO debate

  4. #4
    SitePoint Zealot Serberus's Avatar
    Join Date
    Oct 2005
    Location
    Herts, UK
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I thought the comment previously made on this forum about using them in exceptional / unexpected circumstances is a good rule of thumb.

  5. #5
    SitePoint Zealot
    Join Date
    Oct 2004
    Location
    naperville
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there a rule of thumb for whats exceptional / unexpected at in a web application (cms/shopping cart/invoice/accounting/etc)? I really want some basic examples - hate to be asking such basic questions, but I'm just unaccustomed to exceptions and want to use the best practices possible.

  6. #6
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I might suggest that exceptions be used when the user of the library does not (for whatever reason) want to have a direct connection with of the library's error conditions. This may be because there are several layers between the library and where the exception is handled or several other curcumstances. It certainly adds a layer of abstraction that can make code clearer than passing return values up the call stack and the tangle of code that that can sometimes produce.

    Perhaps the exception mavens here can present some examples of common cases where using exceptions might be considered a best practice in PHP.
    Christopher

  7. #7
    SitePoint Zealot
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    185
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by BerislavLopac
    There is another, very hot thread on this very topic. My position is that yes, you should use them in most cases you mention in the first paragraph. Some seem to disagree, though.
    Quickly tried to scan the current page and missed this link. Would you mind posting it?

    --
    lv

  8. #8
    SitePoint Addict
    Join Date
    Aug 2003
    Location
    Toronto
    Posts
    300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I liked this comment from the php internals mailing list:

    Exceptions are meant for the thrower - to signal that situation does not allow for the current function to continue execution and for the catcher - to deal with the exceptional situation respectively. It's purpose is to handle exceptional situation, not to pass control around.
    The key question to ask is: within a given (try) block, what conditions/states would stop this block from doing valid processing such that handling of such must be defered to another (catch) block? It is tempting to do standard flow-control using exceptions -- IMO that leads to disaster.

  9. #9
    SitePoint Zealot johno's Avatar
    Join Date
    Sep 2003
    Location
    Bratislava, Slovakia
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've read somewhere that "Exceptions should be used when the object does not know how to handle some error situation."

    For example: You are sending mail with some Mailer class. Sending fails. Mailer does not (and should not) know how to handle this situation. Some top script may try use another SMTP server, add that message to queue or just display error some message. It's the top guy's job to handle the error, not the mailers job.

    Try to use this as an analogy for your problem.
    Annotations support for PHP5
    TC/OPT™ Group Leader

  10. #10
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lvismer
    Quickly tried to scan the current page and missed this link. Would you mind posting it?
    I think it's buried somewhere in this thread: http://www.sitepoint.com/forums/showthread.php?t=319966


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •