php异常处理

异常处理的过程:

判断出错,然后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),调用类,把内容写进去

posted @ 2014-10-28 00:50  fengyiqing  阅读(101)  评论(0编辑  收藏  举报