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();
}