此代码:

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相关问答推荐

Flutter 是否提供即时应用程序?

如何使用Flutter在按钮网格中滑动(swipe)/拖动(drag) 2 个或更多按钮

Flutter Dismissible 坚持必须从树中删除列表项

如何在Flatter中zoom 两个谷歌 map 标记

Dart VM 的性能与 Node.js 相比如何?

如何在 Flutter DropDown 按钮中搜索

如何在 Flutter CustomPainter 中使用Bezier Curves绘制形状

在 Dart 中使用带有 Future 的循环

用于名称和数字省道的正则表达式

在 Dart 中实现观察者模式

带有 Dart 的 CORS,我如何让它工作?

对象的默认stringify,相当于Java的toString?

Dart vs Haxe - Current state, hype, usability, ...?

以编程方式使dart中的十六进制 colored颜色 变亮或变暗

如何将 3 字节的 unicode 字符写为字符串文字

使用 VSCode 创建和运行 Dart 控制台应用程序?

如何在 base64 中编码 Dart 字符串?

Dart 中如何将列表转换为map

如何比较 Dart 中的列表是否相等?

如何在构造函数中初始化最终类属性?