| 状态 | 草稿 |
|---|---|
| Todo | triggering errors |
| 官方最后更新时间 | 2008/08/12 13:21 |
实现是通过 Kohana logging methods 把信息记录在日志文件中。
在 index.php 文件中有一个 display_errors 的设置,其功能是否把错误信息显示在屏幕上面(页面)。开发过程中可以选择开启(TRUE)此功能以便调试,如果你的产品已经发布或上线请关闭(FALSE)此功能,可以有效的防止用户看到错误信息。同时它不会影响错误日志。 开发期间你希望此选项设置为TRUE,在正式使用的时候设置FALSE阻止用户看到错误信息,这不会影响错误日志.
异常处理在 Kohana 相当于 Kohana Exception 类的一个句柄,他有三种类型:Kohana_Exception,Kohana_User_Exception 和 Kohana_404_Exception。
Kohana_Exception 继承自 Exception。抛出一个 Kohana_Exception 并且 i18n 语言文件(language file)必须存在。
语法
/** * @param string i18n language key for the message * @param array addition line parameters */ throw new Kohana_Exception(string $i18_lang_key [, string $message]);
实例
//... if($x == 0) throw new Kohana_Exception('math.division_by_zero'); // Throw a $lang['division_by_zero'] exception in ./i18n/en_US/math.php // "Cannot divide by zero." else if($x < 0) throw new Kohana_Exception('general.math.negative_number', $x); // Throw a $lang['math']['negative_number'] exception in ./i18n/en_US/general.php and pass the message $x // "The number passed was negative: -5" //...
Kohana_User_Exception 继承自 Kohana_Exception。这个类似于 Kohana_Exception 异常但其异常信息不需要在 i18n 结构中。
语法
/** * @param string exception title string * @param string exception message string * @param string custom error template */ throw new Kohana_User_Exception(string $title, string $message [, string $template]);
实例
//... if($x == 0) throw new Kohana_User_Exception('Cannot divide by zero', 'You passed a zero'); else if($x < 0) throw new Kohana_User_Exception('Number Type Exception', "Cannot use a negative number $x"); //...
Kohana_404_Exception 继承自 Kohana_Exception。显示 404 错误信息给用户。
语法
/** * @param string URL of page * @param string custom error template */ throw new Kohana_404_Exception([string $page [, string $template]]);
实例
//... if($x == 0) throw new Kohana_404_Exception('divide by zero'); // "The page you requested, Cannot divide by zero, could not be found." //...
默认的错误页面在 system/views/kohana_error_page.php。 你可以在 application/views 目录下重载一个 kohana_error_page.php 或者在相似的模型文件夹。
如果开启的话,默认的错误页面将会显示错误的堆栈跟踪信息。
Kohana 可以记录错误信息(errors),警告信息(alerts),提示信息(info )和调试(debugging )信息,相关设置请参见 application/config/log.php 文件的相关参数配置。