Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Exception Handling in PHP

Posted on 2011-03-04
2
Medium Priority
?
288 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.
Suggested Courses

916 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