Payne's PHP Notes

Back to Payne's PHP Notebook.

9. Exception and Filter

Exception:

Syntax / Example Result
<?php

// Throw
function check($variable)
{
    throw new Exception("Exception message.");
}

// Try
try
{
    check(3);
}

// Catch
catch(Exception $e)
{
    echo "Exception: " . $e->getMessage();
}

?>
Exception: Exception message.

Exception Class:

Syntax / Example Result
<?php

class myException extends Exception
{
    public function errorMessage()
    {
        $errorMsg="Error on line: ".$this->getLine()."<br />";
        $errorMsg.="Error in file: ".$this->getFile()."<br />";
        $errorMsg.="Error message: ".$this->getMessage()."<br />";
        return $errorMsg;
    }
}

$emailAddress="nobody@are...you...kidding...me?";

try
{
    if(filter_var($emailAddress, FILTER_VALIDATE_EMAIL)==FALSE)
    {
        throw new myException($emailAddress);
    }
}

catch(myException $e)
{
    echo $e->errorMessage();
}

?>
Error on line: 145
Error in file: /home1/paynesn1/public_html/IT/PHP/09/index.php
Error message: nobody@are...you...kidding...me?

Other Exception Notes and Concepts:

Filters: