我们的apache error_log最近填充了类似于以下内容的行(约50GB):
[Wed Feb 01 16:50:15 2012] [error] [client 123.123.123.123] PHP Warning:
unpack() [<a href='function.unpack'>function.unpack</a>]:
Type V: not enough input, need 4, have 1
in /var/www/vhosts/domain.com/httpdocs/imagecreatefrombmp.php on line 52
Run Code Online (Sandbox Code Playgroud)
imagecreatefrombmp.bmp中的第52行如下:
$COLOR = unpack("V",substr($IMG,$P,3).$VIDE);
Run Code Online (Sandbox Code Playgroud)
这条线埋在一个while循环中.
如果再次发生此问题,我希望代码安静地退出while循环.
问题是我无法自己复制问题所以我有点需要盲目解决.
我设计了以下小解决方案.它会达到目的吗?如果再次出现"Type V not input ..."错误,try catch块会捕获它并返回false吗?
try{
$COLOR = unpack("V",substr($IMG,$P,3).$VIDE);
}catch (Exception $e) {
return FALSE;
}
Run Code Online (Sandbox Code Playgroud)
您无法捕获 PHP 错误或警告,因为它不是异常。
您可以在调用之后测试error_get_last()unpack是否引发错误,但这并不实际。
另一个解决方案是设置一个错误处理程序来捕获警告,然后抛出ErrorException该警告。然后您将能够使用 try/catch 和return false;.
function my_error_handler($errno = 0, $errstr = null, $errfile = null, $errline = null) {
// If error is suppressed with @, don't throw an exception
if (error_reporting() === 0) {
return true; // return true to continue through the others error handlers
}
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
set_error_handler('my_error_handler');
Run Code Online (Sandbox Code Playgroud)
注意:所有错误、警告、通知等...都将转换为异常。如果您以前有过其中之一,这可能会导致您的程序崩溃。
现在你可以捕获异常:
try {
$COLOR = unpack("V",substr($IMG,$P,3).$VIDE);
} catch (ErrorException $e) {
return false;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3177 次 |
| 最近记录: |