2.自定义错误触发机制
1.
<?
php
2.
function
my_error
(
$errno
,
$errstr
){
3.
echo
"<font color='red'>错误编号:$errno</font> 错误信息:
$errstr"
;
4.
exit
();
5.
}
6.
//第一个参数是自定义错误函数,第二个参数是错误级别
7.
//用户自定义的错误级别就是上面三种
E_USER_ERROR,E_USER_WARNING,E_USER_NOTICE
8.
set_error_handler
(
my_error
,
E_USER_NOTICE
);
9.
$num
=
100
;
10.
if
(
$num
>
80
){
11.
trigger_error
(
"出错了"
,
E_USER_NOTICE
);
12.
}
13. ?>
3.php 异常处理:
(1)当异常被抛出时,其后的代码不会被执行,而是直接跳到 catch,直接执行 catch
里面的内容,如果异常没有被捕获,又没使用 set_exception_handler(),将会发生致
命错误。
看看手册里面的一个例子:
1.
<?
php
2.
function
checkNum
(
$number
){
3.
if
(
$number
>
1
){
4.
throw
new
Exception
(
"Value must be 1 or below"
);
5.
}
6.
return
true
;
7.
}
8.
9. //
在 "try" 代码块中触发异常
10.
try
{
11. checkNum
(
2
);
12. echo
'If you see this, the number is 1 or below'
;
13.
}
catch
(
Exception
$e
){