Source for file Exception.php
Documentation is available at Exception.php
/* vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker: */
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 2004 The PEAR Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 3.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/3_0.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Tomas V.V.Cox <cox@idecnet.com> |
// | Hans Lellelid <hans@velum.net> |
// | Bertrand Mansion <bmansion@mamasam.com> |
// | Greg Beaver <cellog@php.net> |
// +----------------------------------------------------------------------+
// $Id: Exception.php,v 1.4.2.2 2004/12/31 19:01:52 cellog Exp $
* Base PEAR_Exception Class
* WARNING: This code should be considered stable, but the API is
* subject to immediate and drastic change, so API stability is
* - 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) {
* @version $Revision: 1.4.2.2 $
* @author Tomas V.V.Cox <cox@idecnet.com>
* @author Hans Lellelid <hans@velum.net>
* @author Bertrand Mansion <bmansion@mamasam.com>
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, array $causes);
* PEAR_Exception(string $message, array $causes, int $code);
public function __construct($message, $p2 = null , $p3 = null )
} elseif ($p2 instanceof Exception || is_array($p2)) {
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);
foreach ($this->cause as $cause) {
$cause->getCauseMessage ($causes);
} 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 border="1" cellspacing="0">' . "\n";
foreach ($causes as $i => $cause) {
$html .= '<tr><td colspan="3" bgcolor="#ff9999">'
. str_repeat('-', $i) . ' <b>' . $cause['class'] . '</b>: '
. 'on line <b>' . $cause['line'] . '</b>'
$html .= '<tr><td colspan="3" bgcolor="#aaaaaa" align="center"><b>Exception trace</b></td></tr>' . "\n"
. '<tr><td align="center" bgcolor="#cccccc" width="20"><b>#</b></td>'
. '<td align="center" bgcolor="#cccccc"><b>Function</b></td>'
. '<td align="center" bgcolor="#cccccc"><b>Location</b></td></tr>' . "\n";
foreach ($trace as $k => $v) {
$html .= '<tr><td 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>' . $v['file'] . ':' . $v['line'] . '</td></tr>' . "\n";
$html .= '<tr><td 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 Mon, 11 Mar 2019 14:23:57 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|