Main Page

message property

The name of the
Error
object corresponds to its class (because
Error
is just a base class), which is one of
the following:
Class
Occurs When
EvalError
An error occurs in the
eval()
function.
RangeError
A number value is greater than or less than the numbers that can be repre-
sented in JavaScript (
Number.MAX_VALUE
and
Number.MIN_VALUE
).
ReferenceError
An illegal reference is used.
SyntaxError
A syntax error occurs inside of an
eval()
function call. All other syntax
errors are reported by the browser and cannot be handled with a
try...catch
statement.
TypeError
A variable’s type is unexpected.
URIError
An error occurs in the
encodeURI()
or the
decodeURI()
function.
The
message
property of an
Error
object is the browser-generated error message indicating the nature
of the error. Because this property is browser-specific, the same error can generate a different error mes-
sage in different browsers. Consider the following line of code:
eval(“a ++ b”);
This line alone causes a
SyntaxError
to be thrown because the ++ symbol isn’t valid in this context. The
error message from Internet Explorer 6 is
“Expected ‘;’”
whereas Mozilla 1.5 provides
“missing ;
before statement.”
The
message
property can be used to display a more meaningful message to users while preventing the
browser from reporting the error directly:
try {
window.nonExistentFunction();
alert(“Method completed.”);
} catch (oException) {
alert(“An exception occurred: “ + oException.message);
} finally {
alert(“End of try...catch test.”);
}
Both Mozilla and Internet Explorer have extended the
Error
object to suit their own
needs. Mozilla provides a
fileName
property to indicate which file the error occurred
in, a
lineNumber
property indicating the line that the error occurred on, and a stack
property containing the call stack up to the point of the error; Internet Explorer pro-
vides a
number
property to indicate the error number.
425
Error Handling
17_579088 ch14.qxd 3/28/05 11:41 AM Page 425


JavaScript EditorFree JavaScript Editor     Ajax Editor


©