问题
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