35

这段代码:

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)

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

4

1 回答 1

45

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 主要 (file:///home/darshan/so/stacktrace.dart:7:7)
于 2013-04-16T04:22:49.907 回答