PHP教程

PHP抛出简单说明

本文主要是介绍PHP抛出简单说明,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

PHP抛出简单说明 (异常不是错误 关闭错误提示并不影响异常)

1 throw 抛出异常

2 try catch捕获(如果没有 就走自定义异常)

try{

  throw 

} catch (\Throwable $t){

}

 

ps: throwable是 基类 可以代替所有各种异常类型
throw可以自定义异常 

class customException extends Exception
{
    public function errorMessage()
    {
        // 错误信息
        $errorMsg = '错误行号 '.$this->getLine().' in '.$this->getFile()
        .': <b>'.$this->getMessage().'</b> 不是一个合法的 E-Mail 地址';
        return $errorMsg;
    }
}
 
$email = "someone@example...com";
 
try
{
    // 检测邮箱
    if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE)
    {
        // 如果是个不合法的邮箱地址,抛出异常
        throw new customException($email);
    }
}
 
catch (customException $e)
{
//display custom message
echo $e->errorMessage();
}

 

这篇关于PHP抛出简单说明的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!