What throws DOMErrors

1.1k views Asked by At

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?

3

There are 3 answers

3
bobince On BEST ANSWER

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.

2
Jeremy Gehrs On

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.

1
copy On

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.