maxmind / GeoIP2-php

PHP API for GeoIP2 webservice client and database reader
https://maxmind.github.io/GeoIP2-php/
Apache License 2.0
2.33k stars 276 forks source link

Can't catch GeoIp2\Exception\AddressNotFoundException #16

Closed tsolar closed 10 years ago

tsolar commented 10 years ago

Hi guys,

I've been trying by infinite ways to catch this exception.

The exception comes from https://github.com/maxmind/GeoIP2-php/blob/master/src/GeoIp2/Database/Reader.php#L167

This is not catching the exception:

use GeoIp2\Database\Reader;
...
...
try {
    $reader = new Reader(app_path().'/database/maxmind/GeoLite2-City.mmdb');
    $record = $reader->city($ip); // $ip defined before, trying with 234.234.34.4
    print($record->country->isoCode . "\n");

    } catch (Exception $e) {
        echo 'Caught!';
    }
}
...

I've tried a lot of things, but anything works.... :( However, this simple try codeblock I pasted above should work, right?

Am I doing anything wrong? How can I catch that exception?

oschwald commented 10 years ago

That code should work. Is it possible that you have an exception class in your namespace called Exception? You could try changing catch (Exception $e) to catch (\Exception $e).

As a best practice, I would suggest catching GeoIp2\Exception\AddressNotFoundException specifically rather than trying to catch all exceptions.

tsolar commented 10 years ago

Wow, I was focused only in Exception, and I completely forgot to escape it from current namespace.

I had an if clause inside the catch to manage GeoIp2\Exception\AddressNotFoundException, that was my mistake, because it provoked me to unfocus in Exception.

Thank you very much!! You made my day :smiley: