Example 1 — Print a String Stack Trace
Throw from a helper and echo getTraceAsString() from the catch block.
<?php
function failing(): never
{
throw new Exception("String trace demo");
}
try {
failing();
} catch (Exception $e) {
echo $e->getTraceAsString(), PHP_EOL;
} How It Works
- failing() throws the Exception.
- getTraceAsString() formats the call stack as text.
- Frame #0 shows failing().
- The exact formatting can include file paths depending on PHP version and context.

