Jump to content

Throw Exception not working. What I'm doing wrong?


lopes_andre

Recommended Posts

Hi,

 

I'm testing Exceptions in PHP. But I'm not getting this to work.

 

Please see the code.

 

$testar_classe = new geoIpImportCSV('geolitecity', 'http://geolite.maxmind.com/download/geoip/database/GeoLiteCity_CSV/');
// Bloco Try/Catch
try {
echo $testar_classe->deleteFileFromDisk('GeoLiteCity-Location.csv');
}
catch (Exception $e) {
echo $e->getMessage;
}

 

I'm running the code above passing to the method "deleteFileFromDisk" a parameter with a wrong name to show the Exception.

 

The method that I'm calling have this code:

 

public function deleteFileFromDisk($fileNameToBeDeleted) {

	// Apagar ficheiro
	$handle = unlink($fileNameToBeDeleted);

	// Fazer a verificação de excepções
	if (!$handle) {
		// Passou-se algo e o ficheiro n foi apagado.
		throw new Exception("Nao foi possivel apagar o ficheiro. (this->deleteFileFromDisk) - " . $fileNameToBeDeleted);
	}

	// Correu tudo bem, vou sair
	return TRUE;
}

 

What I'm doing wrong? This Exception does not catch wrong filenames passed to the parameter?

 

The result of running the code is this:

Warning: unlink(GeoLiteCity-Location.csv): No such file or directory in C:\xampp
\htdocs\testes\testecsv4.php on line 295

 

Give some clues.

 

Best Regards

Link to comment
Share on other sites

i am not very experienced with exception handling, but have you tried this?

 

		
	// Fazer a verificação de excepções
	if (!unlink($fileNameToBeDeleted)) {
// Passou-se algo e o ficheiro n foi apagado.
		throw new Exception("Nao foi possivel apagar o ficheiro. (this->deleteFileFromDisk) - " . $fileNameToBeDeleted);
	}

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.