Unhandled Exception method

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    Unhandled Exception method

    Having written a error handler class that inherits the Exception class, I am struggling what to do with unhandled exceptions.

    An unhandled exception static method within the error handler class seemed a good idea with set_exception_h andler() in the construct, but this means I cannot call legally that classes methods.

    Can someone suggest a better idea?
    Code:
    class errorHandler extends Exception
    {
        //Constructor
        function __construct($intro='')
        {
    	   //Other stuff	
            set_exception_handler 'errorHandler::unhandledException');
        }
       
        public function handledException()
    	{
                //Collect error information		
                $this->exceptionMethods();
    	}    
        
        public static function unhandledException($e)
    	{
    		errorHandler::exceptionMethods(); #No can do
    	}
    
    private exceptionMethods()
    {
    //Call various exception methods and construct error string
    }
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    Originally posted by code green
    Having written a error handler class that inherits the Exception class, I am struggling what to do with unhandled exceptions.
    just to get on the right track, you want to process caught exceptions with your class? then it doesn't seem right to inherit from Exception (I mean, what is the benefit?)

    one more point:
    set_exception_h andler() is registered as often as you throw an exception…

    what about (though there's no need to put this into a class)
    Code:
    public static function unhandledException($e)
    {
        $e->handleException();
    }

    Comment

    • code green
      Recognized Expert Top Contributor
      • Mar 2007
      • 1726

      #3
      what is the benefit
      I'm sorry. I was tossing a few ideas about and probably submitted the worst of them.

      Basically I am looking for somewhere to put an unhandled exception function. At the moment it is in a global config file included in all scripts.

      I have another error class (not inherited from Exception) that collects all the errors into a string/file/array for later handling, which is instantiated once.

      If I put unhandled exception in this it obviously has to be public static.
      How do I allow this method to access other methods and members in the class.
      Code:
      class errorHandler
      {
          //private members        
          function __construct($intro='')
          {
      		set_exception_handler('errorHandler::unhandledExcept');
          }    
          public static function unhandledExcept($exception)
      	{
              $str = '<br>Unhandled Exception '.
      					'<br>Trap: '.$exception->getMessage().
      					'<br>Code: '.$exception->getCode().
      					'<br>Line: '.$exception->getLine().
      					'<br>File: '.basename($exception->getFile()).
      					'<br>------------------------------');
              //Pass message to handler function
              errorHandler::errormessage($str);#Generates a warning	
              $this->errormessage($str);#Illegal	
      	}.
      How does such a static function interact with the class

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        before I proceed, how do you throw the Exception?

        Originally posted by code green
        How does such a static function interact with the class
        staticly… i.e. the method is part of the class, not of the object (you can't use non-static members).

        Comment

        • code green
          Recognized Expert Top Contributor
          • Mar 2007
          • 1726

          #5
          before I proceed, how do you throw the Exception
          For example
          Code:
          try{//code to open a file
          ............
          else
            throw new Exception('Could not open file '.$filename);
          }
          //Oops no matching catch block. 
          //Hopefully my unHandledExcept will catch it
          You can't use non-static members
          So the function could only work as a standalone.
          and if I want to use the errorHandler class object it would have to be global
          Code:
          highlevelIncludedFile.php
          set_exception_handler('errorHandler::unhandledExcept'); 
          function unhandledExcept($exception) 
              { 
                  $str = '<br>Unhandled Exception '. 
                              '<br>Trap: '.$exception->getMessage(). 
                              '<br>Code: '.$exception->getCode(). 
                              '<br>Line: '.$exception->getLine(). 
                              '<br>File: '.basename($exception->getFile()). 
                              '<br>------------------------------'); 
                  //Pass message to handler class for storage
                  global $errObject;
                 $errObject->errormessage($str);     
          }.
          This currently what I am doing but I don't like the global idea

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            there is no need for errorHandler to extend Exception.

            imagine set_exception_h andler()'s function as a global catch block. so your standard catch code belongs there (at least).

            $errObject does not need to be global. use an OOP pattern that does the same (e.g. Singleton, Registry)

            PS: I can send you my Error Handling Class as an example

            Comment

            • code green
              Recognized Expert Top Contributor
              • Mar 2007
              • 1726

              #7
              there is no need for errorHandler to extend Exception
              That was a lousy idea. My later post shows errorHandler as a non-inheriting class.
              imagine set_exception_h andler()'s function as a global catch block.
              Yes I can. I am trying to find a nice place to put it.
              $errObject does not need to be global. use an OOP pattern that does the same (e.g. Singleton, Registry)
              Pardon?

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                Originally posted by code green
                Pardon?
                In OOP there are Patterns (like predefined code designs to solve a common problem).

                for instance (what a pun…) a class designed after the Singleton Pattern will always return you the same single instance of itself, independendly from where you call it.

                so from my knowledge I can tell that at least the Singleton and Registry Patterns would fit your needs (because I have coded error handling classes myself)

                Comment

                • code green
                  Recognized Expert Top Contributor
                  • Mar 2007
                  • 1726

                  #9
                  PS: I can send you my Error Handling Class as an example
                  That would be a good idea. It is the design concept I need to understand
                  How do we go about that?
                  I will obviously not be posting my email address.

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    Originally posted by code green
                    How do we go about that?
                    I will obviously not be posting my email address.
                    send your mail address it via PM

                    Comment

                    • Markus
                      Recognized Expert Expert
                      • Jun 2007
                      • 6092

                      #11
                      Add it as an attachment, if you like Dormi; I'd like to check it out too :)

                      Comment

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #12
                        one is a customized Exception class used for easily passing the collected data (time, function, error level …) to the handler.

                        one is the storage object

                        one is the handling class using the Abstract Registry Pattern (non-GoF).

                        EDIT: also added the uncaught exception handler, though it looks as if it needs an update…
                        Attached Files

                        Comment

                        Working...