Does the code in the try block continue to execute after an exception?

I am trying to embed the Google+ username on my website and am encountering a problem. Here is my code (after creating the Google_Client object):

try { $client->authenticate($_GET['code']); $plus = new \Google_Service_Plus($client); $person = $plus->people->get('me'); $firstName = $person->modelData->name->givenName; } catch (Google_Auth_Exception $e) { $response = array( 'error' => 'Error: Authentication exception.' ); } catch (Exception $e) { $response = array( 'error' => 'Error: Uncaught exception.' ); } 
  • $client->authenticate() throws a Google_Auth_Exception if the code passed to it is invalid
  • If authentication fails, then reading the properties of the $person object causes fatal errors
  • $response is called as a JSON encoded object

The problem is that the try/catch code is not working properly. When authentication failed due to incorrect code in $_GET['code'] , from the script:

The following answer is returned:
 {"error":"Error: Authentication exception."} 

So far, so good - the code in the first catch block has been executed.

However, the code in the try block continues to execute in a strange way. I say “weird” because in the form above there are a lot of errors (culminating in a fatal error), which means this line:

 $firstName = $person->modelData->name->givenName; 

Still running! It should not be executed because the exception was thrown on the previous line. If I comment on the above line, errors will not be thrown (again indicating that this line has been completed, which should not be).

Here are the errors that were thrown due to the above line that is executed after the exception:

Note: Undefined index: modelData in [...] \ google-api-php-client-master \ src \ Google \ Model.php on line 78

Note. Trying to get a non-object property in [...] \ ajax_handler.php on line 720 [note: this is the line shown above where the object is accessed]

Note: attempt to get non-object property in [...] \ ajax_handler.php on line 720

Another reason I said “weird” is because if I add this line:

 die('dying before reading property'); 

Right before the specified line (where I read the property), no errors occur, BUT the text "skill to read" is not displayed on the page! This is strange because the script obviously still executes the code in the try block after an error occurs (since without this die () line, the line reading the property is executed and leads to a large number of errors being printed). As before, the code in the catch block is still executing, and JSON is output to the page.

What's happening?

+6
source share
2 answers

PHP is a compiled language. See this question and this answer for an explanation.

In principle, the code is parsed, assembled, and compiled into bytecode before it is executed; what happens to your script is that the code is invalid. The syntax is correct, so PHP does not just fail completely. The problem occurs after creating the $person object when trying to access a dynamic property ( modelData ) that does not exist or is not available due to its size. So the errors you see are due to the fact that PHP cannot find it.

The reason the code does not display 'dying before reading property' is because the code is not actually running, so it cannot "stop" execution; he has already stopped.

Change

 $firstName = $person->modelData->name->givenName; 

to

 $firstName = $person->name->givenName; 

and you should be good to go.

+5
source

This question and its selected answer continue to bother me. It seems so unlikely that PHP would execute / parse / anything while the exception has already been thrown.

I would prefer the code to run twice. The first time you send an exception message, and the second time you create notifications.

I suggest you try the following:

  • put $testVar = uniqid(); just before try {
  • put echo $testVar; right after try {

When you run the code, you should now see a unique identifier before the exception message and (my theory) a different / unique unique identifier before your notifications. This proves that the code runs twice.

About die("..."); not appearing, I would ask if you really checked this in your HTML source, because it might be hidden for some reasong. (e.g. invisible div)

+1
source

Source: https://habr.com/ru/post/969854/


All Articles