Dart: unhandledExceptionCallback is ignored

て烟熏妆下的殇ゞ 提交于 2020-01-15 12:32:57

问题


Here is a very simple code that I run using command line dart to demonstrate my point:

import 'dart:isolate';

void isolateMain() {
  throw new Exception("ouch");
}

bool handleException(IsolateUnhandledException e) {
  print("EXCEPTION in isolate: " + e.toString());
  return true;
}

void main() {
  SendPort sendPort = spawnFunction(isolateMain, handleException);
  sendPort.call("Hello").then((e) {
    print("Main received " + e);
  });
}

and the output:

Exception: ouch
#0      isolateMain (file:///Users/salomon/Workspaces/eclipse/Deployer_Server/bin/deployer_server.dart:7:3)

So, turns out the unhandledExceptionCallback is never called whereas the isolate does throw an exception.

For the record :

> dart --version
Dart VM version: 0.5.20.4_r24275 (Fri Jun 21 05:02:50 2013) on "macos_x64"

So, can someone explain me what did I do wrong ?

Thanks ;)


回答1:


I don't know if you did wrong, it could be a bug. But it seems exceptions thrown in the isolate's main function aren't caught by the handler. If you change it like this:

import 'dart:isolate';

void isolateMain() {
  port.receive((whatever, mahPort) {
    throw new Exception("$whatever");
  });
}

bool handleException(IsolateUnhandledException e) {
  print("EXCEPTION in isolate: ${e.toString()}");
  return true;
}

void main() {
  SendPort sendPort = spawnFunction(isolateMain, handleException);
  sendPort.call("Hello").then((e) {
    print("Main received $e");
  });
}

... then handleException() will be called.



来源:https://stackoverflow.com/questions/17292762/dart-unhandledexceptioncallback-is-ignored

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!