PHP 为何会出现 Fatal error: Uncaught exception 错误
PHP 的异常原理
PHP5+ 开始提供了一种新的面向对象的错误处理方法,用于在指定的错误(异常)情况发
生时改变脚本的正常流程。这种情况称为异常。
一般使用方法:
<?php
function
zhoz_com_test() {
throw
new
Exception(
"异常啦"
);
}
try {
zhoz_com_test();
} catch (Exception
$e
) {
echo
$e
->getMessage();
}
throw new Exception('XXX') : 抛出一个异常
try : 使用异常的函数应该位于 "try" 代码块内。如果没有触发异常,则代码将照常继续执
行。但是如果异常被触发,会抛出一个异常。
Catch:代码块会捕获异常,并创建一个包含异常信息的对象
自定义异常类:
class
myException
extends
Exception {
public
function
errorMessage() {
$errorMsg
=
'zhoz.com Error on line '
.
$this
->getLine().
' in '
.
$this
->getFile()
.
': <b>'
.
$this
->getMessage().
'</b> is not a valid E-Mail address'
;
return
$errorMsg
;