Top-level exception handling?

58 views
Skip to first unread message

Michael Gerlek

unread,
Jan 28, 2015, 3:00:44 PM1/28/15
to mi...@dartlang.org
When I'm running my web app, I believe I'm running under the auspices of an isolate that I don't own. So, how do I catch exceptions at the top level?

For example, consider this much-simplified case (pretend it's calling deep down into some 3rd party Javascript I don't own):

<body>
    <button onclick="throw new Error();"></button>
</body>
<script type="application/dart" src="main.dart"></script>

Can I nicely catch and handle the error from within my Dart app?

My inclination is something like a static "Isolate.currentIsolate" getter that I could hang an "onError" on to, but I'm not seeing anything like that anywhere -- so perhaps I'm not thinking about this right way..?

Thanks -

-mpg


Justin Fagnani

unread,
Jan 28, 2015, 9:36:27 PM1/28/15
to General Dart Discussion

--
For other discussions, see https://groups.google.com/a/dartlang.org/
 
For HOWTO questions, visit http://stackoverflow.com/tags/dart
 
To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.

Lasse R.H. Nielsen

unread,
Jan 29, 2015, 3:48:27 AM1/29/15
to mi...@dartlang.org
For most cases, the zone solution is enough. It is possible to leave your zone again (for example by doing `Zone.ROOT.run(() => throw 42)`, which will throw the 42 in the root zone), but there's rarely any reason to do so.

The Isolate.current getter has been added and will probably be in the 1.9 release. If all goes well, the "addErrorListener" on Isolate will also be implemented by then, which should give you what you need to catch any completely uncaught error. 

/L

--
Lasse R.H. Nielsen - l...@google.com  
'Faith without judgement merely degrades the spirit divine'
Google Denmark ApS - Frederiksborggade 20B, 1 sal - 1360 København K - Denmark - CVR nr. 28 86 69 84

Michael Gerlek

unread,
Jan 29, 2015, 8:05:10 AM1/29/15
to mi...@dartlang.org
Great, thanks - that's what I was looking for, will watch for it 1.9. (Justin's solution doesn't apply in my case, since I'm not the one making the zone...)

-mpg
Reply all
Reply to author
Forward
0 new messages