gpt4 book ai didi

dart - 如何重新抛出异常并保留堆栈跟踪?

转载 作者:行者123 更新时间:2023-12-04 01:59:50 26 4
gpt4 key购买 nike

这段代码:

try {
try {
throw 1;
} catch (e, s) {
print("$e $s");
throw e;
}
} catch (e2, s2) {
print("$e2 $s2");
}

打印:
1 #0      main (file:///.../test.dart:34:7)

1 #0 main (file:///.../test.dart:37:7)

所以原始堆栈跟踪完全丢失了。有没有办法在保留堆栈跟踪的情况下重新抛出?

最佳答案

当前版本的 Dart VM 和 dart2js支持重新抛出,保留堆栈跟踪,使用 rethrow :

void main() {
try {
try {
throw 1;
} catch (e, s) {
print("$e $s");
rethrow;
}
} catch (e2, s2) {
print("$e2 $s2");
}
}

这产生:

1 #0 main (file:///home/darshan/so/stacktrace.dart:4:7)

1 #0 main (file:///home/darshan/so/stacktrace.dart:4:7)
#1 main (file:///home/darshan/so/stacktrace.dart:7:7)

关于dart - 如何重新抛出异常并保留堆栈跟踪?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16028565/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com