Page 185 - Computer Software Application TP - Volume 1
P. 185

COMPUTER SOFTWARE APPLICATION - CITS



















           TASK 2: Error Handling using try-catch and throw
           1  Open the text editor

           2  Write the following codes
              <html >
              <head>
              <title> Object Literal Notation </title>
              </head>

              <body>
              <script>
              function divide(a, b)
              {
              if (b === 0)

              {
              throw new Error(“Division by zero is not allowed”);
              }
              return a / b;
              }

              try
              {
              console.log(divide(10, 0));
              }

              catch (error)
              {
              console.error(“An error occurred:”, error.message);
              }
              </script>
              </body>

              </html>
           3  Save the program as a .html file
           4  Open the html file with a web browser
           5  Go to the browsers console tab and verify the output.






                                                           170

                               CITS : IT & ITES - Computer Software Application - Exercise 45
   180   181   182   183   184   185   186   187   188   189   190