繁体   English   中英

我应该如何使用Dart Isolate unhandledExceptionCallback?

[英]How should i use Dart Isolate unhandledExceptionCallback?

我正在尝试在我的Dart webapp中使用Isolates但我似乎无法使错误回调参数工作。 我有一个非常基本的代码,它在Dartium中运行。

import "dart:isolate";

void main() {
  print("Main.");
  spawnFunction(test, (IsolateUnhandledException e) {
    print(e);
  });
}

void test() {
  throw "Ooops.";
}

我从来没有看到过“主要”的东西。 打印在控制台上。 我做错了什么或现在这样做了吗?

错误回调将在新隔离中执行。 因此它不能是动态闭包,而是需要一个静态函数。

我没有测试过,但这应该有效:

import "dart:isolate";

bool errorHandler(IsolateUnhandledException e) {
  print(e);
  return true;
}

void test() {
  throw "Ooops.";
}

void main() {
  // Make sure the program doesn't terminate immediately by keeping a
  // ReceivePort open. It will never stop now, but at least you should
  // see the error in the other isolate now.
  var port = new ReceivePort();
  print("Main.");
  spawnFunction(test, errorHandler);
}

注意:在dart2js中,此功能仍未实现。 旧版本只是忽略了这个论点。 较新的版本将抛出UnimplementedError。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM