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

COMPUTER SOFTWARE APPLICATION - CITS






















           1  Create a JavaScript function that takes two parameters, numerator and denominator, and attempts to divide
              numerator by denominator inside a try block. Use a catch block to handle any division by zero errors and log
              an appropriate error message.
           2  Create a JavaScript program that attempts to execute a non-existent function inside a try block. Use a catch
              block to handle the error and log a message indicating that the function does not exist.

           3  Create a JavaScript function named calculateFactorial that takes a parameter num and calculates the factorial
              of the number. Use a try-catch block to handle the scenario where num is a negative integer. If num is negative,
              throw an error with an appropriate message.
           4  Write a JavaScript program that prompts the user to enter their age. Use a try-catch block to validate that the
              entered age is a positive integer. If the age is not valid, throw an error with a custom message.
           5  Write a JavaScript program that attempts to access a property of an undefined object inside a try block. Use a
              catch block to handle the error and log a custom error message to the console. Include a finally block to show
              some message, regardless of whether an error occurred or not.
















































                                                           172

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