PHP Tutorial PHP Advanced PHP References

PHP Exception - getTrace() Method



The PHP Exception::getTrace() method is used to get the Exception stack trace.

Syntax

final public Exception::getTrace()

Parameters

No parameter is required.

Return Value

Returns the Exception stack trace as an array.

Example: Exception::getTrace() example

The example below shows the usage of Exception::getTrace() method.

<?php
function divide($dividend, $divisor) {
  if($divisor == 0) {
    throw new Exception("Division by zero is invalid.", 25);
  }
  return $dividend / $divisor;
}

try {
  echo divide(25, 0);
} catch(Exception $e) {
  $trace = $e->getTrace();
  print_r($trace);
}
?>

The output of the above code will be similar to:

Array
(
    [0] => Array
        (
            [file] => Main.php
            [line] => 10
            [function] => divide
        )

)

❮ PHP - Exceptions

5