用户
 找回密码
 入住 CI 中国社区
搜索
查看: 5503|回复: 0
收起左侧

kohana文档(Error Handling)

[复制链接]
发表于 2008-7-25 16:35:03 | 显示全部楼层 |阅读模式
Config.display_errors
在index.php中有display_errors的设置,这决定错误是否输出到屏幕,开发期间你希望此选项设置为TRUE,在正式使用的时候设置FALSE阻止用户看到错误信息,这不会影响错误日志.
异常处理
Kohana中的异常处理由Exception控制,有三个类型: Kohana_Exception, Kohana_User_Exception and Kohana_404_Exception.
Kohana_Exception
Kohana_Exception继承自Exception,抛出Kohana_Exception,i18n language文件必须存在
语法
PHP复制代码
/**
 * @param   string  i18n language key for the message
 * @param   array   addition line parameters
 */

throw new Kohana_Exception(string $i18_lang_key [, string $message]);
复制代码



举例:
PHP复制代码
//...
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_User_Exception继承自Exception,和Kohana_Exception相似,只是其异常信息不需要在i18n结构中
语法:
PHP复制代码
/**
 * @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]);
复制代码

举例:
PHP复制代码
//...
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_404_Exception继承自Exception,向用户展示404错误
语法:
PHP复制代码
/**
 * @param string  URL of page
 * @param string  custom error template
 */

throw new Kohana_404_Exception([string $page [, string $template]]);
复制代码

举例:
PHP复制代码
//...
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可以记录错误,调试等信息,可以在application/config/log.php 是设置

[ 本帖最后由 星期八 于 2008-7-25 16:37 编辑 ]

本版积分规则