Jump to content

try catch and how to email error message?


raysefo

Recommended Posts

Hi,

 

I am new to php. I wrote a sample code as follows;

try
{
	//Get Files to Parse
	
	$homepage = file_get_contents('ftp://username:[email protected]:21/RESULTS/test.txt');


	$kw = explode("\n", $homepage);
	$totalNumRow = 0;
	$totalAmount = 0;



	for($i=0;$i<count($kw);$i++){
		
		
		if(substr($kw[$i], 0, 1) == "5" && substr($kw[$i], 1, 1) <> "/")
		{
			// echo $kw[$i];
			
			$totalNumRow = $totalNumRow + 1;
			
			$originalstring = $kw[$i];
			
			$delimiter = ",";
			if(strpos($originalstring,$delimiter) > 0){
					$outarray = explode($delimiter,$originalstring);
					// $variable1 = $outarray[0];
					$amount = $outarray[1];
					// echo $amount;
					$totalAmount = $totalAmount + $amount;
			}
			
			// echo "<br>";
		}
		
		
	}
}
catch (Exception $e)
{
	$msg = $e->getMessage();
	sendEmail($msg);
}

Lets say if the file is not in server ($homepage = file_get_contents('ftp://username:[email protected]:21/RESULTS/test.txt') ;), is there a way to catch and send this error?

 

Best Regards.

You'll need to do some testing but I'd have thought file_get_contents() would return FALSE and not trigger an E_WARNING because you're retrieving a file using the File Transfer Protocol. I'm not 100% sure as to what the result is if it fails though so test it. 

 

Once you've found out what the trigger is, you can do something like:

try {
   if($file === false) {
      throw new Exception("Message");
   }
}
catch(Exception $e) {
   // Handle
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.