note 72373 added to language.exceptions

From: Date: Tue, 16 Jan 2007 17:43:19 +0000
Subject: note 72373 added to language.exceptions
Groups: php.notes 
Request: Send a blank email to php-notes+get-121803@lists.php.net to get a copy of this message
If you need a Java-like exception with a sort of "Throwable" cause to report the whole stackTrace, you can use this class I wrote and I release under the LGPL GNU Lesser General Public Liscence. It looks more or less like the original PHP "Exception" but you might want to change the formatting of the returned string in the two last functions. Enjoy :) <?php /* Author : Romain Boisnard */ /* Liscenced under the LGPL GNU Lesser General Public Liscence, report the actual liscence for details. /* LinkedException */ // Java-like exception with a cause class LinkedException extends Exception { private $cause; function __construct($_message = null, $_code = 0, Exception $_cause = null) { parent::__construct($_message, $_code); $this->cause = $_cause; } public function getCause() { return $this->cause; } public function getStackTrace() { if ($this->cause !== null) { $arr = array(); $trace = $this->getTrace(); array_push($arr, $trace[0]); unset($trace); if (get_class($this->cause) == "LinkedException") { foreach ($this->cause->getStackTrace() as $key => $trace) { array_push($arr, $trace); } } else { foreach ($this->cause->getTrace() as $key => $trace) { array_push($arr, $trace); } } return $arr; } else { return $this->getTrace(); } } public function showStackTrace() { $htmldoc = "<p style=\"font-family: monospace; border: solid 1px #000000\"><span style=\"font-weight: bold; color: #000000;\">An exception was thrown :<br/></span>"; $htmldoc.= "Exception code : $this->code<br/>"; $htmldoc.= "Exception message : $this->message<br/>"; $htmldoc.= "<span style=\"color: #0000FF;\">"; $i = 0; foreach ($this->getStackTrace() as $key => $trace) { $htmldoc.= $this->showTrace($trace, $i); $i++; } $htmldoc.= "#$i {main}<br/>"; unset($i); $htmldoc.= "</span></p>"; return $htmldoc; } private function showTrace($_trace, $_i) { $htmldoc = "#$_i "; if (array_key_exists("file",$_trace)) { $htmldoc.= $_trace["file"]; } if (array_key_exists("line",$_trace)) { $htmldoc.= "(".$_trace["line"]."): "; } if (array_key_exists("class",$_trace) && array_key_exists("type",$_trace)) { $htmldoc.= $_trace["class"].$_trace["type"]; } if (array_key_exists("function",$_trace)) { $htmldoc.= $_trace["function"]."("; if (array_key_exists("args",$_trace)) { if (count($_trace["args"]) > 0) { $args = $_trace["args"]; $type = gettype($args[0]); $value = $args[0]; unset($args); if ($type == "boolean") { if ($value) { $htmldoc.= "true"; } else { $htmldoc.= "false"; } } elseif ($type == "integer" || $type == "double") { if (settype($value, "string")) { if (strlen($value) <= 20) { $htmldoc.= $value; } else { $htmldoc.= substr($value,0,17)."..."; } } else { if ($type == "integer" ) { $htmldoc.= "? integer ?"; } else { $htmldoc.= "? double or float ?"; } } } elseif ($type == "string") { if (strlen($value) <= 18) { $htmldoc.= "'$value'"; } else { $htmldoc.= "'".substr($value,0,15)."...'"; } } elseif ($type == "array") { $htmldoc.= "Array"; } elseif ($type == "object") { $htmldoc.= "Object"; } elseif ($type == "resource") { $htmldoc.= "Resource"; } elseif ($type == "NULL") { $htmldoc.= "null"; } elseif ($type == "unknown type") { $htmldoc.= "? unknown type ?"; } unset($type); unset($value); } if (count($_trace["args"]) > 1) { $htmldoc.= ",..."; } } $htmldoc.= ")<br/>"; } return $htmldoc; } } ?> ---- Server IP: 217.174.196.197 Probable Submitter: 80.170.54.231 ---- Manual Page -- http://www.php.net/manual/en/language.exceptions.php Edit -- https://master.php.net/note/edit/72373 Del: integrated -- https://master.php.net/note/delete/72373/integrated Del: useless -- https://master.php.net/note/delete/72373/useless Del: bad code -- https://master.php.net/note/delete/72373/bad+code Del: spam -- https://master.php.net/note/delete/72373/spam Del: non-english -- https://master.php.net/note/delete/72373/non-english Del: in docs -- https://master.php.net/note/delete/72373/in+docs Del: other reasons-- https://master.php.net/note/delete/72373 Reject -- https://master.php.net/note/reject/72373 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#121803) next »