PHP.ini 的 display_errors = On 或者 Off
代码里 ini_set(‘display_errors’,1) 或者 0
错误报告级别
最佳实践
开发环境下打开错误报告。并且错误报告级别 E_ALL
正式环境一定要关闭错误报告
//显示所有的错误类型 error_reporting(E_ALL); 显示所有的错误类型,除了NOTICE 提示之外 E_ALL ^ E_NOTICE); E_ALL &~ 关闭所有的PHP错误报告 error_reporting(0报告所有的错误类型 error_reporting(-1);
举例
try { 找不到文件 throw new Exception('找不到文件',1); 没有权限访问 Exception('没有权限',2); } catch (Exception $e) { $errno = $e -> getCode(); if($errno == 1){ echo getFile(); }elseif($errno == 2 getLine(); } }
文件异常类
class FileException extends Exception{ public function fileInfo(){ return $this->getMessage(); } } print "open file"; new FileException('file does not exist') { print fileInfo(); }
捕获多个异常
{} 文件不存在的异常 class FileNotExistException extends FileException{} 文件权限异常 class FilePermissionException FileException{} fileHandle(){ 文件不存在 new FileNotExistException('file does not exist'); 文件权限异常 new FilePermissionException('access denied'其他异常 new FileException('other exception'); } { fileHandle(); } catch (FileNotExistException getMessage(); }catch(FilePermissionException ){ catch(FileException getMessage(); }
全局异常处理
<?PHP const FILE_NOT_EXIST = 1权限不够 const FILE_PERMISSION = 2; } new FileException('FilenotExist',FileException::FILE_NOT_EXIST); new FileException('FilePermission',1)">FILE_PERMISSION); } switch ( getCode()) { case FileException::FILE_NOT_EXIST: echo($e->getMessage()); break; case FileException::FILE_PERMISSION: echo ( getMessage()); ; } }catch(echo 'exception'; } ?> ------------------------------------------ <?function defaultExceptionHandle(printf("uncaught exception:%s in file %s on line %d",$e->getMessage(),1)">$e->getFile(),1)">getLine()); } set_exception_handler('defaultExceptionHandle'Exception('tese......'); ?>