Results 1 to 11 of 11
  1. #1

    PHP5 - exceptions

    Hi,

    I'm still getting used to developing in PHP5, with the new features. I have experience with these features in other languages, but not in PHP.

    I am curious how you handle exceptions. Do you create several exception types for various types of errors, or just always use Exception for all exceptions?

    I was thinking of having a few types of exceptions, such as ArgumentException, for example. Also, I'm not sure in some cases if it requires another type of exception or not. For instance, I have a Collection class that encapsolates an array in an object-oriented form. Some of the methods accept an index, and an exception is thrown if an index is given that is out of range. I'm not sure if I should have an OutOfBoundsException or some other type, or just a more general type of Exception.

    Thanks for any help.

  2. #2
    You could extend the built-in Exception class and the usage is pretty similar to other languages (try...catch...). Check out the example given on the documentation page:

    http://www.php.net/exceptions
    123finder.com - Browse/search 4-char domains or dictionary domains

  3. #3
    Join Date
    Oct 2004
    Location
    Brisbane, Australia
    Posts
    259
    You could extend the base Exception class with ones of your own, with the idea of being able to catch the right exception for the error that has occured.

    For example:

    PHP Code:
    <?php

    try {

        
    // Some code in here. If it failed it would throw either an out of bounds or aurguement exception

    } catch ( ArgumentException $ex ) {

        
    // Handle this exception

    } catch ( OutOfBoundsException $ex ) {

        
    // Handle this exception

    } catch ( Exception $ex ) {
     
        
    // This one becomes a sort of catch all

    }

    ?>

  4. #4
    Hi,

    I know I'm capable of extending exceptions, I'm just not so sure when I should do so. It would seem silly to create a new exception for every little type of error, but it also seems silly not to create any of your own type at all. So the question is, where do you draw the line, and for which type of errors do you extend Exception?

    Thanks for the replies.

  5. #5
    Join Date
    Jul 2006
    Posts
    34
    The best time to use different Exceptions is when you actually want to differentiate between errors in different places of your system.

    Say in some code you have some SQL running, then a standard error. You might have an SQL Exception which you handle differently to a general error.

    All in you don't get too pedantic and only implement different exceptions when you find the need arise. A good description is worth more than 1000 different Exception types.

  6. #6
    True. Do you have a set of common exception types that you generally use?

    Thanks for the reply.

  7. #7
    Join Date
    Aug 2001
    Location
    Central USA
    Posts
    200
    I'm not sure if it's the "politically correct" way to do thing, but I am with you. I have one general exception class that I've named 'AppException' to throw all my errors. To differentiate between different errors, I just use different class constants for different error codes. Here's an example:
    PHP Code:
    throw new AppException("Requested component was not found in application!<br />Requested: '".$component."'"AppException::E_404); 
    or:
    PHP Code:
    throw new AppException('You are not authorized to view this page.'AppException::E_AUTH); 
    What you ultimately use is up to you, but I personally hate repetitive code. Making 10+ exceptions that are all essentially the same doesn't make any sense to me. It's too much work and too much repetitive typing (or saving File > Save As...). You may have 2-3 exception classes if the use varies a whole lot (like a DbException or a ViewException), but if it doesn't, don't overcomplicate it. Stick to the KISS principle.
    InvoiceMore - Online Billing & Invoicing
    phpDataMapper - Object-Oriented PHP5 Data Mapper ORM

  8. #8
    Join Date
    Aug 2002
    Location
    Canada
    Posts
    665
    Using a global AppException is extremely bad practice. You should follow standard practice and use the existing exceptions in any programming language when they exist, and create your own error-specific exceptions when they don't. Much is the case with PHP which only provides you with 'Exception' - Java is a much better model for stock-exception reuse.

    Instead of throwing an AppException( 'Value was not found' ); Use a ValueNotFoundException and provide specifics. Doing otherwise is just shooting yourself in the foot.
    circlical - hosting software development
    forums * blog

  9. #9
    Join Date
    Aug 2001
    Location
    Central USA
    Posts
    200
    Quote Originally Posted by Saeven
    Instead of throwing an AppException( 'Value was not found' ); Use a ValueNotFoundException and provide specifics. Doing otherwise is just shooting yourself in the foot.
    How and why? Tell me why I should create 20 different exception classes all with pretty much the same code just to be able to type out 'DescriptionOfMyErrorExpection'. It's repetitive and annoying. I believe the Exception class has a field for an error code for a reason. I could be wrong, so please convince me. I just don't see the point of having all that repetitive code.
    InvoiceMore - Online Billing & Invoicing
    phpDataMapper - Object-Oriented PHP5 Data Mapper ORM

  10. #10
    Join Date
    Oct 2004
    Location
    Brisbane, Australia
    Posts
    259
    It wouldn't be all that repetitive. Just do something like:

    PHP Code:
    class ValueNotFoundException extends Exception {}
    class 
    OutOfRangeException extends Exception {}
    // ... etc 

  11. #11
    Join Date
    Aug 2002
    Location
    Canada
    Posts
    665
    How and why? Tell me why I should create 20 different exception classes all with pretty much the same code just to be able to type out 'DescriptionOfMyErrorExpection'.
    I think that you don't understand exceptions. What if a single method, thread or execution pattern can throw many different exceptions? What are you going to do, place a switch inside your 'global' catch block? What if your program doesn't handle a certain type of exception at a given point?

    example:

    Code:
    try{
    
      //doSomething
    
    }
    catch( IllegalFormatException $x1 ){
    
    }
    catch( NullPointerException $x2 ){
    
    
    }
    If your 'doSomething' later down the development line throws a 'BadArgumentException' which above is not caught, then your program will let you know it wasn't handled properly.

    Text strings should NEVER determine exception types.

    An exception is _not_ an error. It is a calculated mistake which must be handled. If all you want to do is exit from a current programmatical thread, write a method and return from it when execution should cease because of some mistake. Exceptions are _not_ control flow operands.
    circlical - hosting software development
    forums * blog

Posting Permissions

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