<?php
/**
* addUser("小明")注释掉的话,可以输出“操作正常”
*/
header("Content-type: text/html; charset=utf-8"); //修改字符集为utf8
try{//执行
addUser("小明"); //这里异常,下面的代码将不会被执行,所以下面的语句异常,也不会显示错误信息了
updateUser("小王");
echo "操作正常";
}
catch(Exception $e){// $e是系统已经定义好的类
echo "失败信息:".$e->getMessage();//获取错误信息
//file_put_contents自定义日志
file_put_contents('error_win'.date("Ymd").'.log', $e->getMessage()."\r\n", FILE_APPEND); //win换行符
file_put_contents('error_linux'.date("Ymd").'.log', $e->getMessage()."\n", FILE_APPEND); //linux换行符
}
//测试用的函数
function addUser($name){
if($name=="小小"){
}else{
throw new Exception("添加失败");//抛出错误信息
}
}
function updateUser($name){
if($name=="小王"){
}else{
throw new Exception("修改失败");//抛出错误信息
}
}
?>
|