1.

In how many ways a JavaScript code can be included in an HTML file?

Answer»

To handle exceptions in JavaScript, use the try, catch, throw and finally statements.

The try statement DEFINES a block of code to be tested for errors, whereas the catch statement allows defining a block of code to be executed if an error occurs in the try.

The try block MUST be followed by EITHER EXACTLY one catch block or one finally block.

Let us SEE an example of try, catch and finally: 

<html>    <head>       <script>          <!--             function show()             {                var val = 5;                try {                   alert("Value = " + val );                }                catch ( e ) {                   alert("Error = " + e.description );                }                finally {                   alert("Finally block executes!" );                }             }          //-->       </script>    </head>    <body>       <p>Click below:</p>       <form>          <input type="button" value="Display" onclick="show();" />       </form>    </body> </html>

The output on running displays the following button:

On clicking the above button, an alert box is visible: 

No error is there. On clicking “OK”, the “finally” statement executes. The alert box in the “finally” generates the following alert: 



Discussion

No Comment Found