Solved

Exception Handling in PHP

Posted on 2011-03-04
2
281 Views
Last Modified: 2012-06-27
I have some code I would like to run that handle exceptions properly and I was wondering what the correct way to do it would be. I need the script to loop through database values, remove non UTF-8 values, and if there is an error record it in an array for emailing to me with successes and failures.

Here is my code
// DO SOMETHING WITH THE FILE DATA
function getMyFile($home,$away)
{
	$in = file($away);
	$out = fopen($home, "w");
		
	//REPLACE NON UTF-8 CHARACTERS AND INSERT INTO HOME FILE
	foreach ($in as $line) 
	{
		$line = preg_replace('/&(.)(acute|cedil|circ|lig|grave|ring|tilde|uml);/', "$1", $line);
		fputs($out, $line);
	}
}

$errorarray = array();
$feedcount = 0;

// LOOP THROUGH DB TO GET THINGS TO DO
while (loop_through_db_and_get_$home_and_$away)
{
	try
	{
		getMyFile($fetch['home'],$fetch['away']);
		$feedcount++;
	}
	catch (Exception $e)
	{
		$errorarray[$fetch['home']] = $e;
	}
}

// DO SOMETHING WITH THE ERRORS

if(count($errorarray) > 0)
{
	$subject = 'Run But With Errors - ' . $feedcount . " files ran smoothly.\n\n".count($errorarray)." files failed.";
	$body = "Here are the failures.\n\n";
	foreach ($errorarray as $file=>$error)
	{
		$body = $body . "File: " . $file . " failed with the following error.\n\n" . $error . "\n\n\n";
	}
}
else
{
	$subject = 'Run Smoothly - ' . $feedcount . " files.";
	$body = "Have a nice day.";
}

sendMail($subject,$body);

Open in new window


What is happening is that if the $home file does not exist the code stops and echos the error, but does not record the error in the array so the email shows a success.

Please help.

Thanks

Mike
0
Comment
Question by:hungoveragain
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 35035485
http://php.net/manual/en/function.fopen.php
check the value of fopen, if the result is FALSE, react accordingly
function getMyFile($home,$away)
{
	$in = file($away);
	$out = fopen($home, "w");

        if ($out === FALSE)
        {
           // file open failed
        }
        else
        {		
	//REPLACE NON UTF-8 CHARACTERS AND INSERT INTO HOME FILE
	foreach ($in as $line) 
	{
		$line = preg_replace('/&(.)(acute|cedil|circ|lig|grave|ring|tilde|uml);/', "$1", $line);
		fputs($out, $line);
	}
        }
}

Open in new window

0
 
LVL 3

Expert Comment

by:pius_babbun
ID: 35035669
Check this if it is helpful to you
// LOOP THROUGH DB TO GET THINGS TO DO
while (loop_through_db_and_get_$home_and_$away)
{
	if(getMyFile($fetch['home'],$fetch['away']))
	{
		$feedcount++;
	}
	else
	{
		$errorarray[$fetch['home']] 
	}
}

Open in new window

0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question