Code has been added to clipboard!

Custom PHP Exception Class

Example
<?php
  class customException extends Exception {
    public function error_message() {
      //defining the error message
      $error_msg = 'Error caught on line '.$this->getLine().' in '.$this->getFile()
        .': <b>'.$this->getMessage().'</b> is no valid E-Mail address';
      return $error_msg;
    }
  }

  $email = "[email protected]";

  try {
    //checking if
    if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE) {
      //throwing an exception in case email is not valid
      throw new customException($email);
    }
  } catch (customException $e) {
    //displaying a custom message
    echo $e->error_message();
  }
?>