Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What throws DOMErrors

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?

like image 508
Raynos Avatar asked Jan 28 '12 14:01

Raynos


People also ask

Does throw new error stop execution?

The throw statement throws a user-defined exception. Execution of the current function will stop (the statements after throw won't be executed), and control will be passed to the first catch block in the call stack.

What is a DOM exception?

The DOMException interface represents an abnormal event (called an exception) that occurs as a result of calling a method or accessing a property of a web API. This is how error conditions are described in web APIs.


2 Answers

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.

like image 140
bobince Avatar answered Sep 21 '22 04:09

bobince


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.

like image 27
copy Avatar answered Sep 19 '22 04:09

copy