This is documentation for Kohana v2.3.x.

Table of Contents
StatusDraft
Todo triggering errors

Error Handling

See Kohana logging methods for information on accessing log files.

Config.display_errors

In the index.php file there is a setting display_errors. This determines whether errors should be outputted to the screen or not. During development you'd want to set this to TRUE, but in production set this to FALSE so as to prevent users from seeing errors. This won't affect logging of errors.

Exceptions

Exceptions in Kohana are handled by the Kohana Exception classes. There are three types of exceptions: Kohana_Exception, Kohana_User_Exception and Kohana_404_Exception.

Kohana_Exception

Kohana_Exception extends Exception. To throw a Kohana_Exception an i18n language file must exist.

Syntax

/**
 * @param   string  i18n language key for the message
 * @param   array   addition line parameters
 */
throw new Kohana_Exception(string $i18_lang_key [, string $message]);

Example

//...
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 extends the Kohana_Exception. This is similar to Kohana_Exception except that the exception messages do not have to be in the i18n structure.

Syntax

/**
 * @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]);

Example

//...
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 extends Kohana_Exception. This exception will display a 404 error message to the user.

Syntax

/**
 * @param  string  URL of page
 * @param  string  custom error template
 */
throw new Kohana_404_Exception([string $page [, string $template]]);

Example

//...
if($x == 0)
  throw new Kohana_404_Exception('divide by zero');
  // "The page you requested, Cannot divide by zero, could not be found."
//...

Triggering errors

Custom error pages

The system/views/kohana_error_page.php is the default error page. You can overload this one by having a kohana_error_page.php in your application/views directory or in a similar modules directory.

The default error page will show you the error as well as a stack trace if available.

Logging

Kohana can log errors, alerts, info and debugging messages. The setting can be found in application/config/config.php See Logging for more information.