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

COMPUTER SOFTWARE APPLICATION - CITS
















           II  Using a custom exception class

              1  Open the text editor
              2  Write the following codes
                       <html >
              <head>

                  <title> custom exception </title>
              </head>
              <body>
                <?php
              // Define a custom exception class
              class CustomException extends Exception

              {
                  public function errorMessage()
                {
                                     // Custom error message

                                     $errorMsg = ‘Error on line ‘ . $this->getLine() . ‘ in ‘
                      . $this->getFile() . ‘<br/> <b>’ . $this->getMessage() .
                      ‘</b>’;
                                     return $errorMsg;
                  }
              }
              // Function to validate an email address
              function validateEmail($email)
              {
                  // Check if the email is valid

                  if (!filter_var($email, FILTER_VALIDATE_EMAIL))
                {
                                     // Throw a custom exception if the email is not valid
                                     throw new CustomException($email);
                  }
                  // Return true if the email is valid
                  return true;
              }
              // Test the validateEmail function with some email addresses
              try


                                                           324
                                CITS : IT & ITES - Computer Software Application - Exercise 60
   334   335   336   337   338   339   340   341   342   343   344