#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2016
    Location
    Lakewood, WA
    Posts
    219
    Rep Power
    17

    Throwing Exceptions


    Here is a small block of code that works. But is it the "right" way to accomplish throwing the exception?

    PHP Code:
    // Cleans up the mission_key table.
    if ($housekeeking->delete_primary_key() == false) {
        throw new 
    DeleteKeyException("Delete Key Failure");

    Last edited by Arty Zifferelli; June 24th, 2017 at 05:59 PM.
  2. #2
  3. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,125
    Rep Power
    4103
    I'd make your exception try to be more descriptive so you can get a better idea of what failed either when you handle it or log it. For example:
    Code:
    if ($housekeeking->delete_primary_key() == false){
        throw new DeleteKeyException($housekeeking);
    }
    Then include information about $housekeeking and the actual reason for the failure inside the exception message.

    Otherwise it's fine. There's really not any other way to throw an exception other than by using throw.
    Recycle your old CD's



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2016
    Location
    Lakewood, WA
    Posts
    219
    Rep Power
    17
    What happens with placing the $housekeeping instance in the exception brackets?
    PHP Code:
    throw new DeleteKeyException($housekeeking); 
  6. #4
  7. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,125
    Rep Power
    4103
    Nothing special, it's just pass it in as a parameter. Inside the exception you use it to get extra information. Since I have no idea what $housekeeping is I can't so any more details.

    As an example though, here's an exception from one of my projects:
    Code:
    class PropertyNotFoundException extends Exception {
        public function __construct($nm, $obj){
            parent::__construct('Property '.$nm.' does not exist on object type '.get_class($obj));
        }
    }
    Which would be called such as:
    Code:
    class foo {
        public function __get($nm){
            switch ($nm){
                case 'exists':
                    return 'exists';
                default:
                    throw new PropertyNotFoundException($nm, $this);
            }
        }
    }
    The general idea is you pass into your exception some sort of context to help with debugging the issue (if such context is applicable). If your $housekeeping variable has no useful information then don't pass it.

    At the very least "Delete key failed " is a fairly generic message, there's no much point in passing that as parameter. I'd either make it the default and pass nothing or pass something a little more specific, such as "Could not delete housekeeping's primary key".

    Comments on this post

    • codergeek42 agrees
    Recycle your old CD's



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2016
    Location
    Lakewood, WA
    Posts
    219
    Rep Power
    17
    Housekeeping is a class. $housekeeping->whatever() is a function in the class.
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2016
    Location
    Lakewood, WA
    Posts
    219
    Rep Power
    17
    What about creating an instance of another class within the exception class to pass data to, for example a error logging class:
    PHP Code:
    require_once('./library/errorLogClass.php');
    class 
    navigationException extends Exception    {
        private 
    errorLogClass;
        
    // Redefine the exception so message isn't optional
        
    public function __construct($message$code 0Exception $previous null) {
            
    // Make sure everything is assigned properly
            
    parent::__construct($message$code$previous);
            
    // Create error logging class
            
    $this->errorLogClass = new errorLogClass;
            
    // Send the data to the function that passes it to the error logging class
            
    $this->logErrors($message$code);
        }    
        
    // Custom string representation of object
        
    public function __toString() {
            return 
    __CLASS__ ": [{$this->code}]: {$this->message}\n";
        }
        
        public function 
    logErrors($message$code) {
            
    // maybe some code that does something...
            
    $this->errorLogClass->logSomeStuff($message$code);
        }

  12. #7
  13. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,125
    Rep Power
    4103
    I would install an exception handler and do the logging there instead.

    If you want to be able to log exceptions from your catch block as well then create some sort of logging function that you can call from both your catch blocks and the handler.
    Recycle your old CD's



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  14. #8
  15. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2016
    Location
    Lakewood, WA
    Posts
    219
    Rep Power
    17
    If you want to be able to log exceptions from your catch block as well then create some sort of logging function that you can call from both your catch blocks and the handler.
    Than what is the function of the custom exception except to stop execution at a certain point and provide information to the code in the catch block? I mean that's powerful enough as just that, just seems like there should be something more you could shove into it.

    But yes, the logging function should be a separate line of code in the catch block, and I've started looking at exception handlers.
  16. #9
  17. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,125
    Rep Power
    4103
    The main purpose of custom exceptions is to better enable you to selectively catch errors. Many times a custom exception would be as simple as
    Code:
    class MyCustomException extends \Exception {}
    Then you can selectively catch MyCustomException to handle it but let any other exceptions bubble up to either higher level catch or the main handler to stop the script.

    The only thing you really should add to the exception is any information that would aid in either handling that exception or debugging the exception.
    Recycle your old CD's



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  18. #10
  19. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2016
    Location
    Lakewood, WA
    Posts
    219
    Rep Power
    17
    I keep seeing this:
    PHP Code:
    ... \Exception ... 
    The backslash. What for?
  20. #11
  21. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,125
    Rep Power
    4103
    It references the top-level/global namespace. If you were trying to declare your custom exception in your own namespace and didn't include the \ then php would think you were trying to extend an exception class from your namespace and give you an error.
    Code:
    namespace Kicken\Software;
    
    class MyCustomException extends Exception{}
    In the above it would be trying to extend Kicken\Software\Exception which doesn't exist.
    Recycle your old CD's



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud

IMN logo majestic logo threadwatch logo seochat tools logo