Search code examples
phperror-handlingphp4

PHP 4 try catch alternative?


Could someone help me translate this code to 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);
}

Basically when there is a fatal error I need to get the $row['Identifier'] so I know what product is causing the error.

Thanks in advance.

EDIT: I don't know what PHP_open_image_file does, but sometimes I get errors like below, and I need to get the product identifier that is causing the error.

Fatal error: PDFlib error [1016] PDF_open_image_file: Couldn't open JPEG file 'picture/b01_le1x.jpg' for reading (file not found) in /var/www/html/catalogue/pdf_make.php on line 618


Solution

  • Am I correct in assuming you are using PDF_open_image_file() from the pdflib PECL extension?

    If so, then it will never throw an exception on PHP 4. I would assume error states are reported through the result, which is an int and thus probably < 1 in case of errors.

    //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;
    }
    

    I've updated this with file_exists to prevent your fatal error.

    As addendum question, why were you trying to rethrow an exception on PHP4?