try { // some code here that throws an exception } catch (Exception $e) { echo $e->getTraceAsString(); }
class MyException extends Exception { public function __toString() { return $this->getTraceAsString(); } } try { // some code here that throws MyException } catch (MyException $e) { echo $e; }
function myFunction() { throw new Exception("An error occurred"); } try { myFunction(); } catch (Exception $e) { $trace = debug_backtrace(); array_shift($trace); echo $e->getMessage() . " in " . $trace[0]['file'] . " on line " . $trace[0]['line']; }This code example throws an exception from a function and catches it. Then, it uses the "debug_backtrace" function to retrieve the function call stack trace and prints the first function call's file and line number, along with the error message. Package/Library: PHP core language