lopes_andre Posted December 22, 2010 Share Posted December 22, 2010 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 https://forums.phpfreaks.com/topic/222407-throw-exception-not-working-what-im-doing-wrong/ Share on other sites More sharing options...
BlueSkyIS Posted December 22, 2010 Share Posted December 22, 2010 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 https://forums.phpfreaks.com/topic/222407-throw-exception-not-working-what-im-doing-wrong/#findComment-1150416 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.