How can I get around the lack of a finally block in PHP?

前端 未结 7 2169
不思量自难忘°
不思量自难忘° 2020-12-08 18:26

PHP prior to version 5.5 has no finally block - i.e., whereas in most sensible languages, you can do:

try {
   //do something
} catch(Exception ex) {
   //ha         


        
7条回答
  •  独厮守ぢ
    2020-12-08 18:51

    As this is a language construct, you won't find an easy solution for this. You can write a function and call it as the last line of your try block and last line before rethrowing the excepion in the try block.

    Good books argues against using finally blocks for any other than freeing resource as you can not be sure it will execute if something nasty happens. Calling it an irritating hole is quite an overstatement. Believe me, a hell lot of exceptionally good code is written in languages without finally block. :)

    The point of finally is to execute no matter if the try block was successfull or not.

提交回复
热议问题