Gargoyle
Gargoyle

Reputation: 10375

Catch PHP exception

I'm using the UrbanAirship library and importing it into my php script and then using it like so:

use UrbanAirship\Push as P;

// Down inside some method...
try {
    $dev = P\deviceToken('....');
} catch (InvalidArgumentException $e) {
    continue;
}

If I look at the deviceToken method I see them throwing the InvalidArgumentException on bad input. However, when this happens, my script just aborts with the exception, instead of catching it and moving on.

Is there something special I have to do to catch this exception?

Upvotes: 1

Views: 84

Answers (2)

cesarv
cesarv

Reputation: 117

continue;

only works in loops. If you don't want to do anything with the exception, simply leave the catch block empty.

use UrbanAirship\Push as P;

// Down inside some method...
try {
    $dev = P\deviceToken('....');
} catch (InvalidArgumentException $e) {
    // Do nothing
}

Upvotes: 0

David Wyly
David Wyly

Reputation: 1701

I have a feeling that namespaces might be complicating your issue.

When catching an exception inside a namespace it is important that you escape to the global space.

Try this:

use UrbanAirship\Push as P;

// Down inside some method...
try {
    $dev = P\deviceToken('....');
} catch (\InvalidArgumentException $e) {
    continue;
}

Upvotes: 2

Related Questions