Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't catch GeoIp2\Exception\AddressNotFoundException #16

Closed
tsolar opened this issue Jun 23, 2014 · 2 comments
Closed

Can't catch GeoIp2\Exception\AddressNotFoundException #16

tsolar opened this issue Jun 23, 2014 · 2 comments

Comments

@tsolar
Copy link

tsolar commented Jun 23, 2014

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
Copy link
Member

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
Copy link
Author

tsolar commented Jun 24, 2014

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 😃

@tsolar tsolar closed this as completed Jun 24, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants