Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Exception Handling in PHP

Posted on 2011-03-04
2
Medium Priority
?
286 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…
The viewer will learn how to count occurrences of each item in an array.

715 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