w3schools says that exceptions can be strings, integers, booleans, or objects, but the example given doesn't strike me as good practice, since exception type checking is done through string comparison. Is this the preferred method of exception handling in JavaScript? Are there built-in exception types (like NullPointerException)? (if so, what are they, what kind of inheritance do they use, and are they preferred over other options?)
You could try Dean Edwards' Base project. It emulates class inheritance in javascript, so using it would enable you to subclass the Error type. You can't catch different type of errors in javascript (since it is not a strong typed language), but you could determine the type of Error which was thrown, and add logic for that specific type, like so:
try {
// do something
} catch (e) {
if (e instanceof NullPointerError) {
// handle nullpointer exception
} else if (e instanceof RuntimeError) {
// handle runtime exception
} else {
// regular exception
}
}
Of course in this code I assume you have declared the NullPointerError and RuntimeError classes as subclasses of Error.
Section 15.11 of the ECMA-262 specification defines several Error
types which "are thrown as exceptions when runtime errors occur... [they] may also serve as base objects for user-defined exception classes." That spec also defines six other error types which are part of the language.
It does seem preferable to throw exceptions of these types (or types derived from them) since it is easier to determine their type; they also have a built-in message property.
In my own experience, try/catch blocks are somewhat rare and feel truly exceptional as opposed to being unavoidable as in Java.
Exceptions can be anything you like.
In other languages, exceptions should be instances, and then the catch
statement chooses which exceptions to catch and which to let fall through by the class of the instance.
JavaScript, however, does not have this feature. You can only catch all exceptions, and then look at the value to decide what to do with it. If you don't want to handle an exception you have to re-throw the caught value manually. (This inevitably makes catch
statements rather messy.)
You can decide to implement your exceptions as objects and compare them with instanceof
. Or you could have unique opaque values to compare against. Or you can even throw straight strings or numbers if you really want to.
Are there built-in exception types (like NullPointerException)?
Kind of. ECMAScript defines some standard error classes: Error
and its subtypes EvalError
, RangeError
, ReferenceError
, SyntaxError
, TypeError
, URIError
. However there are browser implementation issues, for example try { nonexistentvar; }
gives TypeError
under IE instead of ReferenceError
. And there are many other browser-specific exceptions particularly when dealing with the DOM.
It's not traditional to use these objects directly, and trying to subclass them is messy (since JavaScript doesn't have a class system as such). You would tend to stick to your own defined exceptions for your own apps' use instead. Exceptions are not that widely-used in JavaScript.