说明:
1.扔出的异常(throw exception)如果没有被对应的catch块捕获,则会传递到顶级的异常处理器(如果设置了的话)
2.异常的对象的gettrace()方法可以跟踪是异常是在哪个函数中抛出的
3.我们要处理的异常信息一般有四个: 异常信息(getmessage)、文件位置(getfile)、所在的行(getline)、trace信息(debug_print_backtrace)
4.thinkphp 只有通过两种方式扔出异常: 1.直接扔出(throw new excepiton) 2.通过函数 throw_exception() 扔出
5.异常扔出后,会找catch块或顶级异常处理器,其他代码不会执行了 getmessage(); $trace = $e->gettrace(); //如果通过throw_exception扔出则定位到throw_exception被调用的位置 if($trace[0]['function'] == 'throw_exception'){ $error['file'] = $trace[0]['file']; $error['line'] = $trace[0]['line']; }else{ //直接扔出异常 $error['file'] = $e->getfile(); $error['line'] = $e->getline(); } //调试信息 //$error['trace'] = debug_print_backtrace(); //调用异常模板输出即可 //include c('temp_exception_file'); print_r($error); } } function throw_exception($message){ throw new exception($message); } throw_exception('在throw_exception函数扔出的异常!,会定位到throw_exception调用的位置');
