What throws DOMErrors
Asked Answered
C

3

6

Introduction

DOMError is an interface defined by the DOM.

I would like to check whether a browser supports it. A naive check is

window.DOMError !== undefined

A better check would be to use an action that throws a DOMError and check that

err instanceof DOMError === true

Question

What actions throw DOMErrors?

Curtcurtail answered 28/1, 2012 at 14:14 Comment(2)
Why is the second method better? I typically use the first method for support checks.Managerial
@Managerial because checking window.foo checks it exists. Checking thing works as expected checks it actually works as expected. For example event if window.Event exists you don't know whether new Event("click"); will workCurtcurtail
A
3

A DOMError is a non-exception-based error condition. It will never be thrown, except in as much as an object that implements both DOMError and DOMException might be thrown as a DOMException.

It is expected that the concrete class implementing DOMError and DOMException may be the same in many DOM implementations, but they are separated in the spec for the benefit of languages with bindings to DOMException that would make it hard to re-use the existing implementation. For example languages without native exceptions may have an out-of-band error-signalling channel that can't really be dumped as an object onto a property like a DOMError can.

DOMError as drafted in DOM4 is a trivial placeholder, holding only a name string. It is expected that any specs that build on it will add some properties to encapsulate more useful information.

Currently it is used by the W3 File API for errors in FileReader, which, being an asynchronous interface, doesn't have anywhere useful to throw exceptions. The File API doesn't add any extra properties to DOMError or a subinterface as yet, but both it and the DOM4 spec are likely to undergo changes before they get near Recommendation status.

DOMError as originally introduced in DOM Level 3 Core provided an extended error interface with more in-depth information on where in the document the error occurred. It was intended for the serialiser and parser processes in DOM Level 3 LS, but is included in Core for the use of the document.normalizeDocument method, which also simulates a serialise/parse cycle.

Today's browsers don't have a DOMError because they don't support any of DOM 4, DOM Level 3 LS , or normalizeDocument. But other non-browser DOM implementations may; pxdom for one has the DOM 3 interfaces.

Avebury answered 5/2, 2012 at 17:18 Comment(3)
many modern browsers support most of dom4 (try new Event("click"))Curtcurtail
That only works in the very latest WebKit and Opera (indeed it only appeared in the spec back in September). The other significant new feature would be the DOM manipulation methods (replace() et al) which only turned up in the new January draft and nothing supports them yet. It's way too early even to know for sure what ‘supporting DOM4’ will mean!Avebury
my opinion is that dom4 is the living dom spec just like what html is a living spec. so one can never support dom4Curtcurtail
B
1

It's not implemented in Firefox (source), Chrome 17 has it neither.

The w3 documentation on this is really vague. See this statement:

This interface is intended for other specifications that want to introduce error handling through other means than exceptions. It is expected that the exception types are reused.

As far as I can see, methods throw DOMException. Since this is labled interface (instead of exception), I assume it should be implemented and not used.

This existed in DOM Level 3 too, by the way.

Edit: After reading ThinkingStiff's comment, I am pretty sure it should not be thrown ever. You can not even throw it yourself (Opera 11.52):

Uncaught exception: TypeError: 'DOMError' is not a constructor  
Uncaught exception: TypeError: 'DOMError' is not a function

It's also an interface in Java's DOM implementation: DOMError.

Beckham answered 31/1, 2012 at 19:56 Comment(1)
The object exists in Opera. Still trying to get it to throw an error.Managerial
N
0

DOMErrors are caused when trying to create an invalid DOM element, or passing a non-existent node as an argument to node manipulation methods. In other words, an exception is raised when an operation is impossible to perform.

Example:

document.querySelectorAll("div:foo");

This causes a DOMError when div:foo doesn't exist.

Nidus answered 31/1, 2012 at 19:50 Comment(2)
Opera is the only browser I can find that supports it and it's throwing a DOMException for this, not a DOMError.Managerial
document.querySelectorAll("div:foo") throws a DOMException, not a DOMErrorCurtcurtail

© 2022 - 2024 — McMap. All rights reserved.