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

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

    Abstract Exception Class


    Not to beat a dead horse, but more questions about custom exceptions...

    There's an interesting User Contribution at the official PHP Documentation for extending exceptions.

    If you intend on creating a lot of custom exceptions, you may find this code useful. I've created an interface and an abstract exception class that ensures that all parts of the built-in Exception class are preserved in child classes. It also properly pushes all information back to the parent constructor ensuring that nothing is lost. This allows you to quickly create new exceptions on the fly. It also overrides the default __toString method with a more thorough one.
    Linky to the User Comment => PHP: Exceptions - Manual

    Any comments about this? Looks very useful to my noob eye...
  2. #2
  3. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,026
    Rep Power
    4101
    The interface seems unnecessary. The fact that they throw a new exception within their constructor is weird and causes the first stack frame to be off of where the problem is.

    You can make simple custom exceptions by just extending \Exception without any of that extra code if you want.
    Code:
    class CustomException extends \Exception {}
    
    throw new CustomException('Some message');
    A base class can be useful to provide something generic to catch on if you want to catch your own custom exceptions but not others or want to include some common code.
    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