HTML and CSS Reference
In-Depth Information
Table 7.1
Primary Error Types (JavaScript 1.5+)
Error Name
When It Is Raised
EvalError
If the
eval()
function is used in
an incorrect manner
RangeError
If a numeric variable or
parameter exceeds its allowed
range
ReferenceError
If an invalid reference is used;
e.g., the variable is undefined
SyntaxError
If a syntax error occurs while
parsing code in an
eval()
TypeError
If the type of a variable or
parameter is a valid type
URIError
Raised when
encodeURI()
or
decodeURI()
are passed invalid
parameters
EXAMPLE
7.12
<html>
<head><title>Try/Catch</title>
<script type="text/javascript">
1
try
{
alert("Current balance is $:" + get_balance());
}
2
catch(err)
{
alert("Something went wrong! \n"+
3
err.name
+":"+
err.message
);
}
</script>
</head>
</html>
EXPLANATION
1
The
try
block contains the JavaScript that will be tested for errors.
2
If an error occurred in the
try
block, it would be caught in the
catch
block.
3
The argument,
err
, contains the reason for the error. Without the
try/catch
state-
ments, this example would display a blank page and the error would show up in
the browser's error console. If the exception is caught in your program, it will not
show up in the browser's console window (see Figure 7.21).