14. echo
'Message: '
.
$e
->
getMessage
();
15.
}
16. ?>
使用 set_exception_handler()处理的例子:
1.
<?
php
2.
//设置顶级异常处理器
3.
function
myException
(
$e
){
4.
echo
"错误信息->"
.
$e
->
getMessage
();
5.
}
6.
//修改默认的异常处理器
7.
set_exception_handler
(
"myException"
);
8.
9.
function
func
(
$num
){
10.
if
(
$num
>
100
){
11.
throw
new
Exception
(
"你输入的值过大!
(www.phpddt.com)"
);
12.
}
else
{
13.
echo
"ok"
;
14.
}
15.
}
16. func
(
120
);
17. ?>
创建一个自定义的 Exception 类的例子:
1.
<?
php
2.
class
myException
extends
Exception
{
3.
public
function
errorMessage
(){
4. $errorMsg
=
'Error on line '
.
$this
->
getLine
().
' in '
.
$this
->
getFile
()
5.
.
': <b>'
.
$this
->
getMessage
().
'</b>'
;
6.
return
$errorMsg
;
7.
}
8.
}
9. $num
=
120
;
10.
try
{
11.
if
(
$num
>
100
){
12.
throw
new
myException
(
"数字过大"
);