PHP: Exception::getTrace - Manual (original) (raw)
(PHP 5, PHP 7, PHP 8)
Exception::getTrace — Gets the stack trace
Description
final public Exception::getTrace(): array
Parameters
This function has no parameters.
Return Values
Returns the Exception stack trace as an array.
Examples
Example #1 Exception::getTrace() example
`<?php
function test() {
throw new Exception;
}
try {
test();
} catch(Exception $e) {
var_dump($e->getTrace());
}
?>`
The above example will output something similar to:
array(1) { [0]=> array(4) { ["file"]=> string(22) "/home/bjori/tmp/ex.php" ["line"]=> int(7) ["function"]=> string(4) "test" ["args"]=> array(0) { } } }
Found A Problem?
sam at notmyrealemail dot org ¶
13 years ago
Two important points about this function which are not documented:
1) The trace does not include the file / line at which the exception is thrown; that entry is only recorded in the top-level getFile/Line methods.
2) Elements are returned in 'closest-first' order, e.g. if you have a script x which calls function y which calls function z which throws an exception, then the first trace element will be 'Y' and the second will be 'X'.andreas at cap-systems dot com ¶
15 years ago
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) {
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>t</mi><mi>r</mi><mi>a</mi><mi>c</mi><mi>e</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">trace = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6151em;"></span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">a</span><span class="mord mathnormal">ce</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>e->getTrace();
$result = 'Exception: "';
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>r</mi><mi>e</mi><mi>s</mi><mi>u</mi><mi>l</mi><mi>t</mi><mi mathvariant="normal">.</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">result .= </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">res</span><span class="mord mathnormal">u</span><span class="mord mathnormal">lt</span><span class="mord">.</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>e->getMessage();
$result .= '" @ ';
if($trace[0]['class'] != '') {
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>r</mi><mi>e</mi><mi>s</mi><mi>u</mi><mi>l</mi><mi>t</mi><mi mathvariant="normal">.</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">result .= </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">res</span><span class="mord mathnormal">u</span><span class="mord mathnormal">lt</span><span class="mord">.</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>trace[0]['class'];
$result .= '->';
}
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>r</mi><mi>e</mi><mi>s</mi><mi>u</mi><mi>l</mi><mi>t</mi><mi mathvariant="normal">.</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">result .= </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">res</span><span class="mord mathnormal">u</span><span class="mord mathnormal">lt</span><span class="mord">.</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>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();
12 years ago
The order of the trace starts at the source of the exception and does not include main.
So for example:
<?php
function Bar() {
throw new Exception;
}
function Foo() {
Bar();
}
try {
Foo();
} catch(Exception $e) {
var_dump($e->getTrace());
}
?>
Will output:
array(2) {
[0]=>
array(4) {
["file"]=>
string(21) "/.../test.php"
["line"]=>
int(8)
["function"]=>
string(3) "Bar"
["args"]=>
array(0) {
}
}
[1]=>
array(4) {
["file"]=>
string(21) "/.../test.php"
["line"]=>
int(12)
["function"]=>
string(3) "Foo"
["args"]=>
array(0) {
}
}
}an43 dot bal at gmail dot com ¶
5 years ago
As of PHP 7.4 return values of Exception::getTrace() (and Error::getTrace()) no longer contains "args" keys like debug_backtrace() with default options.
So, return value since 7.4 is like debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS).