Handling Errors - Javascript Language Basics

Javascript examples for Language Basics:Introduction

Introduction

JavaScript uses the try...catch statement to deal with errors.

Handling an Exception

Demo Code

ResultView the demo in separate window

<!DOCTYPE HTML>
<html>
    <head>
        <title>Example</title>
    </head>
    <body>
        <script type="text/javascript">
            try {//from   w w  w.  j ava 2 s .co  m
                var myArray;
                for (var i = 0; i < myArray.length; i++) {
                          document.writeln("Index " + i + ": " + myArray[i]);
                }
            } catch (e) {
                      document.writeln("Error: " + e);
            }
        </script>
    </body>
</html>

Related Tutorials