Some more PHP5-related ruminations: Exceptions

From: Date: Sat, 05 Apr 2003 12:10:29 +0000
Subject: Some more PHP5-related ruminations: Exceptions
Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-14915@lists.php.net to get a copy of this message
The execptions in PHP5 look nice, and should make some of the error handling simpler and more consistent. One thing that the Exception class does not have is the ability to chain them. Tried subclassing it but did not work too well (the filename and line were always wrong), so maybe something like the following could be used in PEAR: namespace PEAR { class Exception { private $message; private $code; private $subexception; function __construct($message=null, $code=null, $e=null) { $this->message = $message; $this->code = $code; $this->subexception = $e; } function getCode() { return $this->code; } function getMessage() { return $this->message; } function getSubException() { return $this->subexception; } function getExceptionTrace() { return "Exception Trace:\n".$this->genTrace(); } function getExceptionStack() { return explode("\n", trim($this->genTrace())); } private function genTrace() { $s = '('.$this->getCode().') '.$this->getMessage()."\n"; if (!is_null($this->getSubException())) { $s .= $this->getSubException()->genTrace(); } return $s; } } } which could be tested with: try { try { try { throw new PEAR::Exception('test exception e1',100); } catch (PEAR::Exception $e) { throw new PEAR::Exception('test exception e2', 200, $e); } } catch (PEAR::Exception $e) { throw new PEAR::Exception('test exception e3',300,$e); } } catch (PEAR::Exception $e) { var_dump($e); echo $e->getExceptionTrace(); var_dump($e->getExceptionStack()); } Comments/ideas/opinions? ===== --- Jesus M. Castagnetto (jcastagnetto@yahoo.com) Research: http://metallo.scripps.edu/ Personal: http://www.castagnetto.org/ __________________________________________________ Do you Yahoo!? Yahoo! Tax Center - File online, calculators, forms, and more http://tax.yahoo.com

« previous php.pear.dev (#14915) next »