PHP.net 下异常的参数对比
自定义类(LogException)的重写(Exception)
<?php
header("content-type:test/html;charset=utf-8");
class LogException extends Exception
{
public function __construct(string $message = "", int $code = 0, Throwable $previous = null)
{
parent::__construct($message, $code);
// 1,error_log error_log记录日志的使用方法和配置 3为文件,1一般为邮件通知
// error_log($this->getMessage(), 3, "G:/error/logexception.log");
// error_log($this->getMessage(), 1, "123456@qq.com");
// 2,Exception::getTraceAsString — 获取字符串类型的异常追踪信息
error_log($this->getTraceAsString(), 3, "G:/error/logexception.log");
}
}
try {
$link = @mysql_connect("localhost", "root", "root");
if (!$link) {
// TODO 3,主动抛出异常错误;这里出现问题,不抛出将由PHP错误机制接管
throw new LogicException("数据库连接失败");
}
} catch (LogException $e) {
echo $e->getMessage();
}