如何从异常对象获取堆栈跟踪?我特别希望提取调用堆栈和行号,给出一个例外。
我试过这个:
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}发布于 2020-08-09 16:46:50
和PHP一样,Exception::getTrace()有一个包含文件、行、函数和args的堆栈跟踪细节数组,除了没有行号或args的入口点(从4.42开始)。
https://stackoverflow.com/questions/63326751
复制相似问题