PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun Jul 21, 2019 2:36 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Thu Apr 26, 2012 12:40 pm 
Offline
Forum Contributor

Joined: Tue Apr 17, 2012 12:57 pm
Posts: 160
Coming from a JAVA background, I have always loved the "finally" block in the try/catch logic and in my opinion is the most important block of the group. I wonder why PHP doesn't define the "finally" block. Is there a particular reason why finally wasn't introduced along with try/catch?

A code can get as simple as this:
Syntax: [ Download ] [ Hide ]
try {
        ....
} catch (Exception $e) {
        ....
        throw new MyApplicationException($e);      
} finally {
         //{this gets called irrespective of exception or not, so you can do something like debug or trace level logging, freeing up resources}
}
 


Instead, without 'finally' you would have to do something like (duplication):
Syntax: [ Download ] [ Hide ]
try {
        //....
} catch (Exception $e) {
        //....
        //{this gets called irrespective of exception or not, so you can do something like debug or trace level logging, freeing up resources}
        throw new MyApplicationException($e);      
}
//{this gets called irrespective of exception or not, so you can do something like debug or trace level logging, freeing up resources}
 


What if the code in "{this gets called irrespective of exception or not, so you can do something like debug or trace level logging, freeing up resources}" throws an exception that you wanna handle, see where I'm getting at?


Top
 Profile  
 
PostPosted: Thu Apr 26, 2012 1:45 pm 
Offline
Forum Contributor
User avatar

Joined: Thu May 11, 2006 8:58 pm
Posts: 305
Location: Utah, USA
The choice to exclude finally seems to be an argument about the purpose of exceptions and their role in flow control. See on a bug report request from 2005. There are some other good comments there as well.

If you are worried about freeing up resources, the normal pattern is to use the `__destruct` magic method. A lot of PHP classes use them to close file handles, sql resources, sql transactions etc. because `__destruct` methods will be called sometime after the Exception, when objects are destroyed.

Specifically in dealing with file and sql resources, php will automatically free them up at the end of the script anyway unless there is a segmentation fault which is really rare.


Top
 Profile  
 
PostPosted: Thu Apr 26, 2012 2:39 pm 
Offline
Forum Contributor

Joined: Tue Apr 17, 2012 12:57 pm
Posts: 160


Top
 Profile  
 
PostPosted: Thu Apr 26, 2012 4:38 pm 
Offline
Forum Newbie

Joined: Sat Mar 03, 2012 6:43 am
Posts: 19
PHP is not Java … don’t get me wrong Java didn’t discovered OOP, but for me working with Java 12 hours a day for many years made me thing in an OOP way. Of course the finally clause is not in the OOP core. Finally with PHP is the logical way to go … that the program will executed and that the exception will not be thrown … so the question might be why there is finally in Java …


Top
 Profile  
 
PostPosted: Fri Apr 27, 2012 11:26 am 
Offline
Forum Contributor

Joined: Tue Apr 17, 2012 12:57 pm
Posts: 160


Top
 Profile  
 
PostPosted: Fri Apr 27, 2012 2:37 pm 
Offline
Forum Contributor
User avatar

Joined: Thu May 11, 2006 8:58 pm
Posts: 305
Location: Utah, USA
You're right. It seems like there is no truly good reason to omit `finally` from PHP. I would certainly vote to put it in.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group