Re: PHP 5 Error Handling

From: Date: Fri, 17 Sep 2004 12:12:04 +0000
Subject: Re: PHP 5 Error Handling
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-36231@lists.php.net to get a copy of this message
Joseph Crawford wrote:
Guys, i have been working with custom exception classes that extend the general exception. Here is some code i have
       public function command($cmd) {
               if($cmd) {
                       $res = fputs($this->connection, $cmd);
                       $this->response = fgets($this->connection, 128);
                       switch($this->getCode()) {
                               case 500:
                                       throw new CommandException($cmd);
                               break;
                       }
               }
       }
       public function selectGroup($group) {
               $this->group = $group;
               if(substr($this->response,0,3) == "200") {
                       THIS IS THE LINE THAT THROWS THE ERROR
                       $this->command("NoSuchCommand\n");
                       $this->response = fgets($this->connection, 1024);
               }
               $info = split(" ", $this->response);
               $this->first = $info[2];
               $this->last = $info[3];
       }
now when the error is thrown and i do $e->getLine(); it shows the line of the file where the throw statement is. Is there a way to make it show the actual line number of the line that is the error? the $this->command("NoSuchCommand\n"); line.
one way to do this is to call $e->getTraceAsString() and then log/print the result. It will return whole call stack. But this is kind of overkill. another way:
        public function selectGroup($group) {
                $this->group = $group;
                if(substr($this->response,0,3) == "200") {
                        THIS IS THE LINE THAT THROWS THE ERROR
try {
	                	$this->command("NoSuchCommand\n");
} catch (CommandException $e) { throw new CommandException($e->getCmd_or_whatever_returns_cmd()) }
                        $this->response = fgets($this->connection, 1024);
                }
                $info = split(" ", $this->response);
                $this->first = $info[2];
                $this->last = $info[3];
        }
-- Ognyan Bankov ogre_bank@yahoo.com ogre.homelinux.org

« previous php.db (#36231) next »