Try - 使用异常的函数应该位于 “try” 代码块内。如果没有触发异常,则代码将照常继续执行。但是如果异常被触发,会抛出一个异常。 
Throw - 这里规定如何触发异常。每一个 “throw” 必须对应至少一个 “catch” 
Catch - “catch” 代码块会捕获异常,并创建一个包含异常信息的对象
class customException extends Exception {
    public function errorMessage() {
        $errorMsg = $this->getMessage() . ' is not a valid E-Mail address.';
        return $errorMsg;
    }
}

$email = "someone@example.com";
try {
    
    try {
//check for “example” in mail address 
        if (strpos($email, "example") !== FALSE) {
//throw exception if email is not valid throw new 
            throw new Exception($email);
        }
    } catch (Exception $e) {
//re-throw exception throw new 
       throw new customException($email);
    }
} catch (customException $e) {
//display custom message 
    echo $e->errorMessage();
}
如果异常没有被捕获,而且又没用使用 set_exception_handler() 作相应的处理的话,那么将发生一个严重的错误(致命错误),并且输出 “Uncaught Exception” (未捕获异常)的错误消息。
function myException($exception) {
    echo "Exception:" . $exception->getMessage();
}

set_exception_handler('myException');
throw new Exception('Uncaught Exception occurred'); 

 

 

 
                    
            
                

相关文章:

  • 2021-12-13
  • 2022-12-23
  • 2021-11-24
  • 2022-12-23
  • 2022-02-03
  • 2021-08-21
  • 2021-08-04
  • 2021-12-06
猜你喜欢
  • 2022-12-23
  • 2021-09-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-29
相关资源
相似解决方案