时间:2021-07-01 10:21:17 帮助过:2人阅读
PHP的异常是什么?
PHP 5 提供了一种新的面向对象的错误处理方法。异常处理用于在指定的错误(异常)情况发生时改变脚本的正常流程。这种情况称为异常。
一般使用方法:
getMessage(); }
Catch:代码块会捕获异常,并创建一个包含异常信息的对象
自定义异常类:
class myException extends Exception {
public function errorMessage() {
$errorMsg = 'Error on line '.$this->getLine().' in '.$this->getFile()
.': '.$this->getMessage().' is not a valid E-Mail address';
return $errorMsg;
}
}
try {
throw new myException($email);
}catch (myException $e){
echo $e->errorMessage();
}
Fatal error: Uncaught exception 'myException' in D:\AppServ\www\cctv\trunk\index.php:12 Stack trace: #0 {main}
thrown in D:\AppServ\www\cctv\trunk\index.php on line 12
PHP默认异常类详细:
class exception
{
protected $message = 'Unknow exception'; //自定义的异常信息
protected $code = 0; //定义的异常代码
protected $file; //发生异常的PHP程序名
protected $line; //发生异常的PHP行号
//用于传递用户自定义异常信息和用户自定义异常代码的构造函数
function __construct($message=null,$code=0);
final function getMessage();
final function getCode();
final function getFile();
final function getLine();
final function getTrace(); //以数组形式返回异常传递的路线
final function getTraceAsString(); //返回格式化成字符串的getTrace函数信息
function __toString(); //可重载,用于返回可输出的字符串
}
PHP异常类的作用: