Solved

Exception Handling in PHP

Posted on 2011-03-04
2
278 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 142

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
spacing 5 30
PHP loop not working 4 32
delete database record with modal 21 31
php variable basic question 12 27
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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.

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now