Source for file Exception.php
Documentation is available at Exception.php
/* vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker: */
* @author Tomas V. V. Cox <cox@idecnet.com>
* @author Hans Lellelid <hans@velum.net>
* @author Bertrand Mansion <bmansion@mamasam.com>
* @author Greg Beaver <cellog@php.net>
* @copyright 1997-2009 The Authors
* @license http://opensource.org/licenses/bsd-license.php New BSD License
* @version CVS: $Id: Exception.php 313023 2011-07-06 19:17:11Z dufuz $
* @link http://pear.php.net/package/PEAR
* @since File available since Release 1.3.3
* Base PEAR_Exception Class
* - Nestable exceptions (throw new PEAR_Exception($msg, $prev_exception))
* - Definable triggers, shot when exceptions occur
* - Pretty and informative error messages
* - Added more context info available (like class, method or cause)
* - cause can be a PEAR_Exception or an array of mixed
* PEAR_Exceptions/PEAR_ErrorStack warnings
* - callbacks for specific exception classes and their children
* - Maybe a way to define a 'template' for the output
* 3) Inherited properties from PHP Exception Class:
* 4) Inherited methods from PHP Exception Class:
* require_once 'PEAR/Exception.php';
* throw new PEAR_Exception('Error Message', ERROR_CODE);
* function myLogger($pear_exception) {
* echo $pear_exception->getMessage();
* // each time a exception is thrown the 'myLogger' will be called
* // (its use is completely optional)
* PEAR_Exception::addObserver('myLogger');
* } catch (PEAR_Exception $e) {
* @author Tomas V.V.Cox <cox@idecnet.com>
* @author Hans Lellelid <hans@velum.net>
* @author Bertrand Mansion <bmansion@mamasam.com>
* @author Greg Beaver <cellog@php.net>
* @copyright 1997-2009 The Authors
* @license http://opensource.org/licenses/bsd-license.php New BSD License
* @version Release: 1.9.4
* @link http://pear.php.net/package/PEAR
* @since Class available since Release 1.3.3
private static $_observers = array ();
private static $_uniqueid = 0;
* - PEAR_Exception(string $message);
* - PEAR_Exception(string $message, int $code);
* - PEAR_Exception(string $message, Exception $cause);
* - PEAR_Exception(string $message, Exception $cause, int $code);
* - PEAR_Exception(string $message, PEAR_Error $cause);
* - PEAR_Exception(string $message, PEAR_Error $cause, int $code);
* - PEAR_Exception(string $message, array $causes);
* - PEAR_Exception(string $message, array $causes, int $code);
* @param string exception message
* @param int|Exception|PEAR_Error|array|nullexception cause
* @param int|nullexception code or null
public function __construct($message, $p2 = null , $p3 = null )
// using is_object allows both Exception and PEAR_Error
if (is_object($p2) && !($p2 instanceof Exception )) {
if (is_array($p2) && isset ($p2['message'])) {
// fix potential problem of passing in a single warning
parent ::__construct ($message, $code);
* @param mixed $callback - A valid php callback, see php func is_callable()
* - A PEAR_Exception::OBSERVER_* constant
* - An array(const PEAR_Exception::OBSERVER_*,
* @param string $label The name of the observer. Use this if you want
* to remove it later with removeObserver()
public static function addObserver($callback, $label = 'default')
self ::$_observers[$label] = $callback;
unset (self ::$_observers[$label]);
* @return int unique identifier for an observer
return self ::$_uniqueid++;
private function signal ()
foreach (self ::$_observers as $func) {
if (is_callable ($func)) {
case self ::OBSERVER_PRINT :
$f = (isset ($func[1 ])) ? $func[1 ] : '%s';
printf($f, $this->getMessage ());
case self ::OBSERVER_TRIGGER :
$f = (isset ($func[1 ])) ? $func[1 ] : E_USER_NOTICE;
case self ::OBSERVER_DIE :
$f = (isset ($func[1 ])) ? $func[1 ] : '%s';
die (printf($f, $this->getMessage ()));
* Return specific error information that can be used for more detailed
* error messages or translation.
* This method may be overridden in child exception classes in order
* to add functionality not present in PEAR_Exception and is a placeholder
* The returned array must be an associative array of parameter => value like so:
* array('name' => $name, 'context' => array(...))
* Returns the exception that caused this exception to be thrown
* @return Exception|arrayThe context of the exception
* Function must be public to call on caused exceptions
'message' => $this->message,
if (isset ($trace[0 ]['file'])) {
$cause['file'] = $trace[0 ]['file'];
$cause['line'] = $trace[0 ]['line'];
$this->cause->getCauseMessage ($causes);
} elseif ($this->cause instanceof Exception ) {
'message' => $this->cause->getMessage (),
'file' => $this->cause->getFile (),
'line' => $this->cause->getLine ());
'message' => $this->cause->getMessage (),
foreach ($this->cause as $cause) {
$cause->getCauseMessage ($causes);
} elseif ($cause instanceof Exception ) {
$causes[] = array ('class' => get_class($cause),
'message' => $cause->getMessage (),
'file' => $cause->getFile (),
'line' => $cause->getLine ());
$causes[] = array ('class' => get_class($cause),
'message' => $cause->getMessage (),
} elseif (is_array($cause) && isset ($cause['message'])) {
// PEAR_ErrorStack warning
'class' => $cause['package'],
'message' => $cause['message'],
'file' => isset ($cause['context']['file']) ?
$cause['context']['file'] :
'line' => isset ($cause['context']['line']) ?
$cause['context']['line'] :
if (!isset ($this->_trace)) {
$this->_trace = $this->getTrace ();
if (empty ($this->_trace)) {
$this->_trace = array ($backtrace[count($backtrace)-1 ]);
return $trace[0 ]['class'];
return $trace[0 ]['function'];
if (isset ($_SERVER['REQUEST_URI'])) {
$html = '<table style="border: 1px" cellspacing="0">' . "\n";
foreach ($causes as $i => $cause) {
$html .= '<tr><td colspan="3" style="background: #ff9999">'
. str_repeat('-', $i) . ' <b>' . $cause['class'] . '</b>: '
. 'on line <b>' . $cause['line'] . '</b>'
$html .= '<tr><td colspan="3" style="background-color: #aaaaaa; text-align: center; font-weight: bold;">Exception trace</td></tr>' . "\n"
. '<tr><td style="text-align: center; background: #cccccc; width:20px; font-weight: bold;">#</td>'
. '<td style="text-align: center; background: #cccccc; font-weight: bold;">Function</td>'
. '<td style="text-align: center; background: #cccccc; font-weight: bold;">Location</td></tr>' . "\n";
foreach ($trace as $k => $v) {
$html .= '<tr><td style="text-align: center;">' . $k . '</td>'
if (!empty ($v['class'])) {
$html .= $v['class'] . $v['type'];
if (!empty ($v['args'])) {
foreach ($v['args'] as $arg) {
if (is_null($arg)) $args[] = 'null';
elseif (is_array($arg)) $args[] = 'Array';
elseif (is_bool($arg)) $args[] = $arg ? 'true' : 'false';
if (strlen($arg) > 16 ) $str .= '…';
$args[] = "'" . $str . "'";
$html .= '(' . implode(', ',$args) . ')'
. '<td>' . (isset ($v['file']) ? $v['file'] : 'unknown')
. ':' . (isset ($v['line']) ? $v['line'] : 'unknown')
$html .= '<tr><td style="text-align: center;">' . ($k+1 ) . '</td>'
. '<td> </td></tr>' . "\n"
foreach ($causes as $i => $cause) {
$causeMsg .= str_repeat(' ', $i) . $cause['class'] . ': '
. $cause['message'] . ' in ' . $cause['file']
. ' on line ' . $cause['line'] . "\n";
return $causeMsg . $this->getTraceAsString ();
Documentation generated on Wed, 06 Jul 2011 23:30:49 +0000 by phpDocumentor 1.4.3. PEAR Logo Copyright © PHP Group 2004.
|