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

On Fri, May 31, 2013 at 3:28 PM, johnjbarton <johnjbarton@chromium.org>wrote:

> When JS calls EventTarget dispatchEvent(evt), EventListener-s are called.
> If those EventListeners throw exceptions, what should happen if the closest
> enclosing try/catch block surrounds the dispatchEvent() call?
>
> Both Chrome and Firefox currently ignore the try/catch block and report
> that the exception is "uncaught". In Chrome, this EventListener is invoked
> within an internal try/catch (V8::TryCatch) and any exceptions that result
> are reported as uncaught before returning to the caller (eventually
> returning to the caller of dispatchEvent()).
>
> I could not find any mention in
> http://www.w3.org/TR/DOM-Level-2-Events/events.html of the expected
> result.
>

This behavior is intentional.  Note that the spec you're reading is over a
decade old, the spec you want is http://dom.spec.whatwg.org.  Specifically,
see http://dom.spec.whatwg.org/#dispatching-events.

I think exceptions from callbacks are handled explicitly in specs, eg. by
saying something like "if the called function throws an exception, ...".
Since the "invoke the event listeners" algorithm (step 4.7) doesn't say to
do anything with exceptions, that means that nothing must be done with
them, eg. discard them.  (Anne: correct?)  Maybe this could use a note,
though.

-- 
Glenn Maynard

Received on Sunday, 2 June 2013 18:11:13 UTC