Java Reference
In-Depth Information
Error Objects
An
error
object can be created by the host environment when an exception occurs, or it
can be created in the code using a constructor function, like so:
var error = new Error();
This constructor function takes a parameter that's used as the error message:
error = new Error("Oops, something went wrong");
There are six more error objects used for specific errors:
•
EvalError
is not used in the current ECMAScript specification and only retained
for backwards compatibility. It was used to identify errors when using the global
eval()
function.
•
RangeError
is thrown when a number is outside an allowable range of values.
•
ReferenceError
is thrown when a reference is made to an item that doesn't ex-
ist. For example, try calling the function
unicorn()
in the console (assuming you
haven't defined a function called
unicorn()
).
•
SyntaxError
is thrown when there's an error in the code's syntax.
•
TypeError
is thrown when there's an error in the type of value used; for example,
a string is used when a number is expected.
•
URIError
is thrown when there's a problem encoding or decoding the URI.
These error functions can also be used as constructors to create custom error objects:
error = new TypeError("You need to use numbers in this
function")
All
error
objects have a number of properties, but they are often used inconsistently across
browsers. The only properties that are generally safe to use are:
