JavaScript Tutorial/Development/Exception

Материал из Web эксперт
Версия от 08:24, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Array index to large exception

<html>
<head>
<title>Try Catch Example</title>
<script type="text/javascript">
try {
   var arrErrors = new Array(10000000000000000000000);  //causes error
   arrErrors.push(exception);
} catch (oException) {
    alert("An exception occurred.");
} finally {
    alert("All done.");
}
</script>
</head>
<body>
</body>
</html>


Check Exception type with instanceof operator

<html>
<head>
<title>Try Catch Example</title>
<script type="text/javascript">
try {
    eval("a ++ b");        //causes SyntaxError
} catch (oException) {
    if (oException instanceof SyntaxError) {
        alert("Syntax Error: " + oException.message);
    } else {
        alert("An unexpected error occurred: " + oException.message);
    }
}
</script>
</head>
<body>
</body>
</html>


Check the exception name and output exception message

<html>
<head>
<title>Try Catch Example</title>
<script type="text/javascript">
try {
    eval("a ++ b");        //causes SyntaxError
} catch (oException) {
    if (oException.name == "SyntaxError") {
        alert("Syntax Error: " + oException.message);
    } else {
        alert("An unexpected error occurred: " + oException.message);
    }
}
</script>
</head>
<body>
</body>
</html>


Exception handler will bypass the statements

<html>
<head>
<title>Try Catch Example</title>
<script type="text/javascript">
try {
    window.nonExistentFunction();
    alert("Method completed.");
} catch (oException) {
    alert("An exception occurred: " + oException.message);
} finally {
    alert("End of try�catch test.");
}
</script>
</head>
<body>
</body>
</html>


Get Exception message

<html>
<head>
<title>Try Catch Example</title>
<script type="text/javascript">
try {
    window.nonExistentFunction();
    alert("Method completed.");
} catch (oException) {
    alert("An exception occurred: " + oException.message);
} finally {
    alert("End of try�catch test.");
}
</script>
</head>
<body>
</body>
</html>


Get Exception name

<html>
<head>
<title>Try Catch Example</title>
<script type="text/javascript">
try {
    window.nonExistentFunction();
} catch (oException) {
    alert("An exception occurred: " + oException.name);
} finally {
    alert("End of try�catch test.");
}
</script>
</head>
<body>
</body>
</html>


Syntactic exception

<html>
<head>
<title>Try Catch Example</title>
<script type="text/javascript">
try {
    eval("a -==-++ b");        //causes error
} catch (oException) {
    alert("An exception occurred.");
} finally {
    alert("All done.");
}
</script>
</head>
<body>
</body>
</html>


Throw exception out of a function

<html>
<head>
<title>Try Catch Example</title>
<script type="text/javascript">
function addTwoNumbers(a, b) {
    throw new Error("Two numbers are required.");
}
try {
    result = addTwoNumbers(90);
} catch (oException) {
    alert(oException.message);
}
</script>
</head>
<body>
</body>
</html>