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

COMPUTER SOFTWARE APPLICATION - CITS
























           TASK 3: Error Handling using try-catch-finally
           1  Open the text editor
           2  Write the following codes
              <html >
              <head>
              <title> Object Literal Notation </title>
              </head>
              <body>
              <script>
              try {
              // Code that may throw an error
              let x = 1;
              let y = x + z; // ReferenceError: z is not defined
              console.log(“This line will not be executed if an error occurs.”);
              }
              catch (error)
              {
              // Handle the error
              console.error(“An error occurred:”, error.message); // Output: An error   occurred: z is not defined
              }
              finally
              {
              // Code that runs regardless of whether an error occurred or not
              console.log(“This line will always be executed.”);
              }

              </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.








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