Dart:unhandledExceptionCallback被忽略 [英] Dart: unhandledExceptionCallback is ignored

查看:275
本文介绍了Dart:unhandledExceptionCallback被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常简单的代码,我使用命令行dart来演示我的观点:

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);
  });
}

并输出:

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

所以,结果是unhandledExceptionCallback从不被调用,而隔离程序确实抛出异常。

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

对于记录:

> 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 ?

谢谢;)

推荐答案

我不知道你是否做错了,它可能是一个错误。但看起来异常抛出的孤立的主要功能不被处理程序捕获。如果你这样改变它:

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");
  });
}

... handleException c $ c>将被调用。

... then handleException() will be called.

这篇关于Dart:unhandledExceptionCallback被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆