/*
throw new Exception($error_message);
*/
class customExceptionClass extends Exception
{
public function errorMessage()
{
$errorMsg = 'Error on line: ' . $this->getLine() . "<br />" .
'in file ' . $this->getFile() . "<br />" .
'Error Message: ' . $this->getMessage() . "<br />";
return $errorMsg;
}
}
$email = "test#email.com";
try
{
if (filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE) {
throw new customExceptionClass("email " . $email . " is not valid");
}
if(strpos($email, "test") !== FALSE) {
throw new Exception("$email is a test email");
}
}
catch (customExceptionClass $e)
{
echo $e->errorMessage();
}
catch(Exception $e)
{
echo $e->getMessage();
}
/*
run:
Error on line: 22
in file C:\xampp\htdocs\workingframe.com\test.php
Error Message: email test#email.com is not valid
*/