JavaScript Tutorial/Development/assert
Assert method
<html>
<head>
<title>Debug Example</title>
<script type="text/javascript">
function assert(bCondition, sErrorMessage) {
if (!bCondition) {
alert(sErrorMessage);
throw new Error(sErrorMessage);
}
}
function aFunction(i) {
assert(false, "1==2");
}
aFunction(1);
</script>
</head>
<body>
<P>This page uses assertions to throw a custom JavaScript error.</p>
</body>
</html>
Catch exception throwed from assert function
<html>
<head>
<title>Try Catch Example</title>
<script type="text/javascript">
function assert(bCondition, sErrorMessage) {
if (!bCondition) {
throw new Error(sErrorMessage);
}
}
try {
assert(1 == 2, "Two numbers are required.");
} catch (oException) {
alert(oException.message); //outputs "Two numbers are required."
}
</script>
</head>
<body>
</body>
</html>