Re: Exceptions in event listeners triggered by dispatchEvent().

On Tue, Jun 4, 2013 at 11:22 AM, Bjoern Hoehrmann <derhoermi@gmx.net> wrote:

> * John Barton wrote:
> >Consider the following code, assuming one of the listeners throws:
> >----
> >   try {
> >     targetElement.dispatchEvent(evt);
> >   } catch(exc) {
> >     console.log("A listener had a error");
> >     return;
> >   }
> >   goOnToGlory();
> >----
> >
> >How can a developer know that the catch block can never be executed and
> yet
> >exceptions in handlers are reported as uncaught? The only other experience
> >that I know about which accounts with these two facts is that
> >dispatchEvent() schedules listener executions on another event turn.  But
> >it doesn't, it's just another wacko Web behavior we have to look up and
> >memorize.
>
> http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-EventTarget
> is where I have learned under which conditions dispatchEvent raises an
> exception, on top of the usual reasons, like running out of memory. It
> does not mention listeners being terminated by uncaught exceptions and
> I would not otherwise expect exceptions to propagate through dispatch-
> Event, because one broken listener should not break other listeners so
> the exception cannot be allowed to propagate through `dispatchEvent`.
>

All you are saying here is that you know the implementation of
dispatchEvent(). That's great, and now so do I. That's not the same as
having the behavior as part of the spec and documentation, nor the same as
Web developer's expectations.

I came upon this issue in a project that defines its UI by inheriting from
HTML Elements. That project (trace-viewer) uses (DOM) events as a key part
of its model-view updates. As I understand it, this is the approach
advocated by the Web components work. So I guess more web devs will run
into this issue in future. Hopefully they will find this discussion and all
of the helpful information useful.

jjb


> --
> Björn Höhrmann · mailto:bjoern@hoehrmann.de · http://bjoern.hoehrmann.de
> Am Badedeich 7 · Telefon: +49(0)160/4415681 · http://www.bjoernsworld.de
> 25899 Dagebüll · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
>

Received on Tuesday, 4 June 2013 18:32:47 UTC