码迷,mamicode.com
首页 > Web开发 > 详细

php异常处理

时间:2014-10-28 01:57:50      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   io   color   os   ar   sp   strong   

异常处理的过程:

判断出错,然后throw new Exception(‘自己填写输出的message‘,错误编号code)→

通过

1 try{
2      //将要测试的代码        
3 }catch(Exception $e){           //$e为throw过来的对象$error
4       $e->getMessage();//new Exception对象的message
5       $e->getCode();//new Exception对象的code
6     
7 }

Exception是系统的异常处理类

Exception {
/* 属性 */
protected string $message ;
protected int $code ;
protected string $file ;
protected int $line ;
/* 方法 */
public __construct ([ string $message = "" [, int $code = 0 [, Exception $previous = NULL ]]] )
final public string getMessage ( void )
final public Exception getPrevious ( void )
final public int getCode ( void )
final public string getFile ( void )
final public int getLine ( void )
final public array getTrace ( void )
final public string getTraceAsString ( void )
public string __toString ( void )
final private void __clone ( void )

}

通常用的方法是——getMessage()、getLine()、getCode()、getFile().

 

对异常通常用一个日志类errlog记录起来

1 class errlog{
2       public static function logs($err){
3             $fp=fopen(‘文件路径‘,‘打开方式‘);//打开文件准备装东西
4             $err=date(‘y-m-d   h-i=s‘)."\r\n".$err;//把时间也记录下来,txt文件的换行用\r\n
5             fwrite(‘写进的路径fp‘,‘$err‘)
6     } 
7 
8 }

在catch下errlog::logs($err),调用类,把内容写进去

php异常处理

标签:des   style   blog   io   color   os   ar   sp   strong   

原文地址:http://www.cnblogs.com/fengyiqing/p/4055643.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!