Throw DOMExceptions where appropriate (fixes #2518).