SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    $books++ == true matsko's Avatar
    Join Date
    Sep 2004
    Location
    Toronto
    Posts
    795
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post Heavy Exception Coding

    Back when PHP was in version four, it didn't provide an Exception Handling Support so error handling was performed by error constants or an error fetching method.

    Exception handling is very useful, but how should you structure your exception handling? Should you create various extended exception classes or should you use class constants to filter an exceptions meaning?

    PHP Code:
    class Register {
       const 
    ERROR_FAILED_INPUT 1;
       const 
    ERROR_FAILED_QUERY 2;

       public function 
    send($data) {
            if( !
    dataIsValid($data) )
            throw new 
    Exception('invalid data',self::ERROR_FAILED_INPUT);

            if( !
    dataSaved($data) )
            throw new 
    Exception('failed save query',self::ERROR_FAILED_QUERY);
       }
    }

    try {
        
    $r = new Register();
        
    $r->send($data);
    }
    catch(
    Exception $e) {
        
    $htmlErrorMessage '';
        if(
    $e->getCode()==Register::ERROR_FAILED_INPUT)
        
    $htmlErrorMessage 'You did not enter all your information';
        if(
    $e->getCode()==Register::ERROR_FAILED_QUERY)
        
    $htmlErrorMessage =  'Unable to save information, please try again later';

    Or should you simply create various extended custom Exception classes that are simply used to branch the catch statements.

    PHP Code:
    InvalidInputException extends Exception { }
    InvalidFailureException extends Exception { }
    catch(
    InvalidInputException $e) {
       
    $htmlErrorMessage 'You did not enter all your information';
    }
    catch(
    QueryFailureException $e) {
       
    $htmlErrorMessage 'Unable to save information, please try again';

    The latter has a heavier process footprint due to the extension of the Exception class.

    ...

    I'm not looking for a simple yes or no answer, I just wondering, for those of whom have encountered a similar dilemma, which one in practice works better? What are the pros and cons of either one?
    I can't believe I ate the whole thing

  2. #2
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by matsko View Post
    The latter has a heavier process footprint due to the extension of the Exception class.
    Did you research that, or are you just guessing? I would guess that the latter is fast in terms of clock cycles, but I also don't care much about it, since the difference is microscopic.

  3. #3
    SitePoint Addict
    Join Date
    Oct 2006
    Posts
    210
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The Standard PHP Library defines a collection of exceptions (see http://www.php.net/~helly/php/ext/spl/main.html). The advantage of defining a specific exception is that your code can catch only the exception types that it can handle.

    For example, my database library defines a DbException class which is thrown if a database error (ex., SQL error, server connection lost, etc.) occurs. Most of my code that performs a database operation wouldn't know how to handle these types of errors. So they don't catch them. This allows a web page level routine to catch them, log the error to inform me of the problem, and inform the user that their request couldn't be processed.

    mikem

  4. #4
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by matsko View Post
    I'm not looking for a simple yes or no answer
    I think there is a simple answer: the second. Using custom exception types is much more elegant. This is how the exception mechanism is designed to work. Catchers can either listen out for specific types of exception or for more general ones, as required. Catchers can even re-throw exceptions as another type as exceptions bubble up through the stack.

  5. #5
    SitePoint Addict
    Join Date
    Feb 2007
    Posts
    251
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Extending is better in my opinion, since it allows for polymorphism. Usually conditional logic in OO code is a sign that you have some refactoring to do.

  6. #6
    PHP/Rails Developer Czaries's Avatar
    Join Date
    May 2004
    Location
    Central USA
    Posts
    806
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to extend, so you can more easily filter the Exceptions you want to catch. You can always use multiple catch blocks to handle different Exceptions in the same code:

    PHP Code:
    try {
        
    $r = new Register();
        
    $r->send($data);
    }
    catch(
    InputException $e) {
        
    $htmlErrorMessage 'You did not enter all your information';
    }
    catch(
    QueryException $e) {
        
    $htmlErrorMessage =  'Unable to save information, please try again later';


  7. #7
    SitePoint Enthusiast
    Join Date
    Aug 2008
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I concur; extend, sir.
    TheDrunkenEpic - my ramblings


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
  •