0

I'm running the following code:

    try {

        $count = $user[0]->followers_count;

    } catch (Exception $e) {

        return 'error';
    }

    dd('continuing execution');

Which outputs this:

FatalErrorException in Twitter.php line 174: Cannot use object of type stdClass as array

Now, a quote from https://stackoverflow.com/a/13955721/2724978

First of all one should make clear that an exception is only fatal if it is not caught. Catching an exception does not halt script execution.

My code is not continuing the execution even though it's on a try/catch block. I'm pretty sure I'm missing something, what is it?

7
  • 1
    You are returning in the catch that will stop the script. Commented Oct 24, 2016 at 16:54
  • 1
    I have to assume $user is an object and not an array so try $count = $user->followers_count; Commented Oct 24, 2016 at 16:57
  • it would output 'error' then.. I commented that line, still same output. Commented Oct 24, 2016 at 16:57
  • it returns from an external API in multiple formats, I'm not trying to debug this, I'm trying to keep it running in spite of possible bugs. Commented Oct 24, 2016 at 16:59
  • Ok Is any of these lines line 174 of Twitter.php Commented Oct 24, 2016 at 16:59

1 Answer 1

0

Well, the answer I found is that in PHP, Errors and Exceptions are different things.. Exceptions can be caught and code will not halt. Errors such as FatalErrorException, on the other hand, will always halt the application even if it's in a try/catch block.

No way around it, have to write code for checking variable types.

Also, another thing.. Sometimes (in laravel for example) you gotta use \Exception instead of just Exception.

Sign up to request clarification or add additional context in comments.

1 Comment

Take a look on the example on this page: php.net/manual/en/language.exceptions.php About the \Exception needed in some files, those are instances where your code declared a different namespace (keyword use), as in this: php.net/manual/en/language.namespaces.importing.php If no namespace was declared, then you don't need the trailing \ before builtin class names.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.