Hacklang:如何从异常中获取堆栈跟踪?

Hacklang: how to get stacktrace from Exception?

如何从异常对象中获取堆栈跟踪?给定异常,我特别希望提取调用堆栈和行号。

我试过这个:


function do_it(int $x, int $y): void {
  try {
    $result = $x / $y;
  }

  catch (\Exception $ex) {
    echo "Caught an Exception\n";
    $ex::getTrace();
  }
}

<<__EntryPoint>>
function main(): void {
  do_it(100, 0);
}

但我得到了输出:

Caught an Exception

Fatal error: Uncaught exception 'BadMethodCallException' with message 'Non-static method Exception::getTrace() cannot be called statically' in /Users/navyazaveri/hack_stuff/first.hack:9
Stack trace:
#0 /Users/navyazaveri/hack_stuff/first.hack(15): do_it()
#1 (): main()
#2 {main}

Exception::getTrace(),就像在 PHP 中一样,有一个包含文件、行、函数和参数的堆栈跟踪详细信息数组,除了没有行号或参数的入口点 (截至 4.42).