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

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);
	}

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.