note 98339 added to exception.gettrace
| From: | andreas at cap-systems dot com | Date: | Wed, 09 Jun 2010 19:55:28 +0000 |
| Subject: | note 98339 added to exception.gettrace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-169995@lists.php.net to get a copy of this message | ||
When calling getTrace(), there is also the name of the class in returned array:
<?php
class Test {
function __construct() {
throw new Exception('FATAL ERROR: bla bla...');
}
}
try {
$obj = new Test();
} catch(Exception $e) {
var_dump($e->getTrace());
}
?>
Will show something like:
array(1) {
[0]=> array(6) {
["file"]=> string(54) "/....../test.php"
["line"]=> int(37)
["function"]=> string(11) "__construct"
["class"]=> string(4) "Test"
["type"]=> string(2) "->"
["args"]=> array(0) { }
}
}
You can use this function to format a exception:
<?php
function MakePrettyException(Exception $e) {
$trace = $e->getTrace();
$result = 'Exception: "';
$result .= $e->getMessage();
$result .= '" @ ';
if($trace[0]['class'] != '') {
$result .= $trace[0]['class'];
$result .= '->';
}
$result .= $trace[0]['function'];
$result .= '();<br />';
return $result;
}
//Example:
try {
$obj = new Test();
} catch(Exception $e) {
echo MakePrettyException($e);
}
?>
Result:
Exception: "FATAL ERROR: bla bla..." @ Test->__construct();
----
Server IP: 217.160.235.204
Probable Submitter: 217.231.196.5
----
Manual Page -- http://www.php.net/manual/en/exception.gettrace.php
Edit -- https://master.php.net/note/edit/98339
Del: integrated -- https://master.php.net/note/delete/98339/integrated
Del: useless -- https://master.php.net/note/delete/98339/useless
Del: bad code -- https://master.php.net/note/delete/98339/bad+code
Del: spam -- https://master.php.net/note/delete/98339/spam
Del: non-english -- https://master.php.net/note/delete/98339/non-english
Del: in docs -- https://master.php.net/note/delete/98339/in+docs
Del: other reasons-- https://master.php.net/note/delete/98339
Reject -- https://master.php.net/note/reject/98339
Search -- https://master.php.net/manage/user-notes.php