PHP 4尝试捕捉替代?

时间:2010-11-16 23:27:15

标签: php error-handling php4

有人可以帮我翻译这段代码到PHP 4吗?

try
{
  $picture = PDF_open_image_file($PDF, "jpeg", $imgFile, "", 0); // This is the original statement, this works on PHP4
}
catch(Exception $ex)
{
  $msg = "Error opening $imgFile for Product $row['Identifier']";
  throw new Exception($msg);
}

基本上当出现致命错误时,我需要获取$ row ['Identifier']所以我知道导致错误的产品。

提前致谢。

编辑:我不知道PHP_open_image_file是做什么的,但有时我会收到如下错误,我需要获取导致错误的产品标识符。

  

致命错误:PDFlib错误[1016]   PDF_open_image_file:无法打开   JPEG文件'picture / b01_le1x.jpg'   阅读(未找到文件)   /var/www/html/catalogue/pdf_make.php   在618行

2 个答案:

答案 0 :(得分:3)

假设您使用pdflib PECL扩展程序中的PDF_open_image_file(),我是否正确?

如果是这样,那么它永远不会在PHP 4上抛出异常。我会假设通过结果报告错误状态,这是一个int,因此可能< 1如果有错误。

//try
if (file_exists($imgFile)) {
    $picture = PDF_open_image_file($PDF, "jpeg", $imgFile, "", 0);
}

//catch
if (!$picture) {
   $msg = "Error opening $imgFile for Product $row['Identifier']";
   print $msg;
}

我已使用file_exists 更新以防止您发生致命错误。

作为附录问题,你为什么试图在PHP4上重新抛出异常?

答案 1 :(得分:0)

您可以通过设置默认错误处理程序(请参阅PHP Manual entry)来捕获一些问题,但这不会让您捕获E_ERRORS。

我不认为这在PHP4中是可能的,你需要升级到PHP5,因此它抛出异常而不是E_ERROR。您可以在发生之前捕获某些错误 - 例如在输入文件上运行file_exists(),但您不太可能想到并捕获PDFLib将会发生的所有错误。