How keywod 'finally' is intended for use in PHP

So, I read about exceptions today in the online PHP manual and understand that I have yet to understand the purpose or real need of the finally keyword. I read a few posts here, so my question is slightly different.

I understand that we can finally use this way:

function hi(){ return 'Hi'; } try { throw new LogicException("Throw logic \n"); } catch (InvalidArgumentException $e) { echo $e->getMessage(); } echo hi(); 

output:

 Fatal error: Uncaught LogicException: Throw Logic in C:\Users\...a.php:167 Stack trace: #0 {main} thrown in C:\Users\...a.php on line 167 

So, in this case, the function hi (); not executed for a good reason. I understand if the exception is not handled, the PHP interpreter stops the script. OK. So far, I'm reading, finally, allowing us to execute the hi () function; even if the exception is not handled (although I don't know why)

So, I understand that.

 try { throw new LogicException("Throw logic \n"); } catch (InvalidArgumentException $e) { echo $e->getMessage(); }finally{ echo hi(); } 

output:

 Hi Fatal error: Uncaught LogicException: Throw Logic in C:\Users\...a.php:167 Stack trace: #0 {main} thrown in C:\Users\...a.php on line 167 

In this case, there should be an exception error, as well as a “hi” message from this function, even those that I do not know for this. But I don’t understand this, even if we catch a LogicException with catch (LogicException $e) , and there were no exceptions, we would see what the function would look like and we would see a “hi” message. as in this example

 try { throw new LogicException("Throw logic \n"); } catch (LogicException $e) { echo $e->getMessage(); }finally{ echo hi(); } 

exits

 // Throw logic // Hi 

So, we still see the hi() function being executed, even if we have no Uncaught exceptions. Why and for what purpose? I thought the final block should have been used as a last resort in case exceptions were not caught, even if it wasn’t, then why use it?

+6
source share
3 answers

The finally block should be used when you want certain code to execute regardless of whether an exception has been thrown.

One of the most common applications that I see is to close the database connection - you want it to happen every time (with or without exception), so you don’t get into a hanging connection that blocks the database server from accepting a new one connections.

Consider this pseudo code:

 try { $database->execute($sql); } catch (Exception $exception) { $logger->log($exception->getMessage()); throw $exception; } finally { $database->close(); } 
+2
source
 try { throw new LogicException("Throw logic \n"); -> LogicException thrown } catch (InvalidArgumentException $e) { -> LogicException not catched echo $e->getMessage(); }finally{ echo hi(); -> code executed. "Hi" printed out } LogicException is here -> Fatal error 

therefore in this case:

 try { throw new LogicException("Throw logic \n"); -> LogicException thrown } catch (InvalidArgumentException $e) { -> LogicException not catched echo $e->getMessage(); }finally{ echo hi(); -> code executed die(); } 

no fatal error will be caused due to the expression die and the last option:

 try { throw new LogicException("Throw logic \n"); -> LogicException thrown } catch (InvalidArgumentException $e) { -> LogicException not catched echo $e->getMessage(); } catch (LogicException $e) { -> LogicException catched echo $e->getMessage(); }finally{ echo hi(); -> code executed } 
+1
source

Finally, it should contain any code that needs to be executed regardless of whether an exception exists or not.

Without final:

 try { $handle = fopen("file.txt"); //Do stuff fclose($handle); return something; } catch (Exception $e) { // Log if (isset($handle) && $handle !== false) { fclose($handle); } } 

Finally,

 try { $handle = fopen("file.txt"); return something; } catch (Exception $e) { // Log } finally { if (isset($handle) && $handle !== false) { fclose($handle); } } 

It offers a little decluttering in case you need to free the resource after the function returns.

This becomes even more useful in the following case:

  try { $handle = fopen("file.txt"); if (case1) { return result1; } if (case2) { return result2; } if (case3) { return result3; } if (case4) { return result4; } } finally { if (isset($handle) && $handle !== false) { fclose($handle); } } 

In this case, you can shorten all the necessary fclose calls before each return to one fclose call, which will be executed before the method returns, but after any other code.

0
source

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


All Articles