且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何抛出异常并保留堆栈跟踪?

更新时间:2023-11-19 11:12:07

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

Current versions of the Dart VM and dart2js support rethrowing, preserving the stack trace, with 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)