阮一峰的IT笔记:首页 -> 分类 -> PHP 查看所有文章:按分类 | 按月份

PHP的错误处理

1. 错误报告

error_reporting(E_ALL & ~E_NOTICE)

2. 手工抛出一个错误

trigger_error(message,[error_type]);

可选参数error_type必须是E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE中的一个。

3. 指定错误处理函数

set_error_handle(callback_function,[int error_type])

选填的error_type用来指定所要处理的错误类型。

[错误处理函数的范例]

function my_error_handle($in_errno,$in_errstr,$in_errfile,$in_errline,$in_errcontext)
{
{
$errs = array(
2 => 'E_WARNING',
8 => 'E_NOTICE',
256 => 'E_USER_ERROR',
512 => 'E_USER_WARNING',
1024 => 'E_USER_NOTICE',
);

$err_type = '';
foreach ($errs as $val => $errstr)
{
if (($in_errno & $val) != 0)
{
$err_type .= "$errstr ";
}
}

echo <<









We're sorry, but an error has occurred.

$err_type:($in_errfile, line $in_errline)

$in_errstr

EOTABLE;

// exit on errors, continue otherwise.
if ($in_errno == E_USER_ERROR)
exit;
}


}

调用范例:

set_error_handler('my_error_handler');

4. 调用错误日志函数

error_log(message,delivery_type,destination,email_headers);

第二个参数0表示写入系统日志,1表示发送Email,3表示写入用户指定的文件。

[范例]

error_log($error_message,3,'../logs/auth.log');
header('Location:/login.php?err=1');
exit;

5. 打开错误显示

ini_set(‘display_errors’,‘on’);

6. 抛出一个例外

throw new Exception('Day value must be between 0 and 6');

7. 捕捉一个例外

try
{
$day = get_day_of_week($dayvalue);
}
catch (Exception $e)
{
echo 'An exception was thrown: ' . $e->getMessage();
}

« 十步解决php utf-8编码(转贴) | 首页 | 如何销毁一个session? »

About

This page contains a single entry from the blog posted on 2007年10月31日 20:08.

The previous post in this blog was 十步解决php utf-8编码(转贴).

The next post in this blog is 如何销毁一个session?.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.33

Post a comment