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:pass@111.11.11.111: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:pass@111.11.11.111:21/RESULTS/test.txt') ;), is there a way to catch and send this error?

 

Best Regards.

Link to comment
Share on other sites

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
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.