JavaScript Statement: throw
[this page | pdf | back links]
In JavaScript,
the throw statement (often used
in conjunction with the try … catch
statement) implements error handling. It throws an exception (technically an Error
object).
The exception can be
specified as just some text (e.g. throw
"Error") or a number (e.g. throw
100) or more generally an error
object, e.g. throw new
Error(100,"Error").
EXAMPLE:
HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>
table,th,tr,td {border: 1px solid black; border-collapse: collapse;}
</style>
</head>
<body>
<table>
<tr>
<th>Example</th>
<th>Resulting value of <code>x</code></th>
</tr>
<tr>
<td><code id="Example"></code></td>
<td><code id="Result"></code></td>
</tr>
</table>
<script>
function crSp(n) { return '<br>' + (' ').repeat(n) }
var x = "";
try {
throw new Error(200,"random error");
}
catch(e) { x = e.message; }
document.getElementById("Example").innerHTML =
'var x = "";'
+ crSp(0) + 'try {'
+ crSp(2) + 'throw new Error(200,"random error");'
+ crSp(0) + '}'
+ crSp(0) + 'catch(e) { x = e.message; }'
document.getElementById("Result").innerHTML = x;
</script>
</body>
</html>
|
NAVIGATION LINKS
Contents | Prev | Next | JavaScript Reserved Words