You can use 'finally' for example to close a file connection
It does have it's uses is what I'm saying I suppose
NopeDoes php5 support finally?![]()
| SitePoint Sponsor |





You can use 'finally' for example to close a file connection
It does have it's uses is what I'm saying I suppose
NopeDoes php5 support finally?![]()



The point of finally is that it is executed in BOTH cases:Originally Posted by ghurtado
If a non-handled exception, "throw new UnhandledException();" was thrown, the catch block wouldn't handle it. But the finally block would still be run.PHP Code:try {
print "This is try code."
throw new Exception("An exception");
print "This is never executed.";
} catch (SomeExceptionsButNotOthers $e) {
print "An Exception occurred that was handled";
} finally {
print "This appears if an exception occurred, or if one did not.";
}
print "This only runs if the exception is caught and handled. If the exception is not handled, this code is never reached.";
=Austin
PS: It's not in there:
See: this thread for painful details.Originally Posted by Andi
There's at least one site cached by Google that makes the claim that finally is supported, but
the ultimate documentation contains no mention of finally.
The recommended work-around seems to be "catch (Exception $e)" -- using the most generic possible
catch to serve as a default catcher. This means that you get messy code, of course, or duplicated
code (even worse IMO).
Far be it from me to call the Zend guys morons in a public forum, but «quack, quack»



Here's a way to get the "finally" behavior in PHP5.Originally Posted by Austin_Hastings
=AustinPHP Code:function finally_like()
{
try // FINALLY
{
try // CATCH
{
throw_something();
}
catch (SomeException $se)
{
handle_some_exception($se);
if (not_completely_handled($se))
{
throw $se;
}
// else fall out and throw non-exception below
}
throw new NotReallyAnException("Execute 'finally' block");
}
// FINALLY
catch (Exception $e)
{
do_finally_stuff($e);
if (!isa($e, "NotReallyAnException"))
{
//Real exception not handled by catch block above -- pass along.
throw $e;
}
}
}



Which is why I fail to understand what the difference is betweenOriginally Posted by Austin_Hastings
andPHP Code:try {
$someObject->method();
} catch (someException $e) {
echo "Exception caught";
} finally {
echo "You will always see this";
}
PHP Code:try {
$someObject->method();
} catch (someException $e) {
echo "Exception caught";
}
echo "You will always see this, even without finally";
Garcia





When an exception is caught, isn't the script execution immediately halted? 'Finally' would also btw catch the exception if you decided to throw it, in the example I gave earlier, you'd still want to close the file handling connection?
I don't know why Zend forgot to add in 'finally' but I suppose they must have had their reasons, much like we do not have namespaces in PHP5 either![]()





Take a less simplistic example:Originally Posted by ghurtado
vsPHP Code:function myFunct() {
$someObject->method();
echo "You will not see this";
}
try {
myFunct();
} catch (someException $e) {
echo "Exception caught";
}
If $someObject->method(); throws an exception in the first example, there is no echo. In the second, there will be, because it will get run by the finally after (or before?) the exception is handled.PHP Code:function myFunct() {
try {
$someObject->method();
} finally {
echo "You will always see this";
}
}
try {
myFunct();
} catch (someException $e) {
echo "Exception caught";
}
Douglas
Hello World



Ghurtado,Originally Posted by ghurtado
What happens if an exception that is NOT a "someException" is thrown?
In the first cast, the finally is run as part of "unwinding the stack". In the second case, the exception rockets through the function to a higher level, not stopping for anything.
=Austin



Am I misunderstanding something, or is there not such a thing as finally in PHP?
EDIT: Question should be, there isn't any finally construct in PHP, right?



There is no finally in PHP. There is active pushback (see my earlier post) to suggestions that it be added. Ergo, there will likely not be a finally in PHP for some time.Originally Posted by Kilroy
=Austin



Has anyone tried to use to following?
This would be a nice way to catch any exceptions that are not handled, and in the function you could instantiate a new PageElement/View and then show it.PHP Code:function exception_handler($exception) {
echo "Uncaught exception: " . $exception. "\n";
}
set_exception_handler('exception_handler');
throw new Exception('Uncaught Exception');
echo "Not Executed\n";
When i try to use this code my apache crashes with a 404 Error, can anyone confirm this?



That code outputs:
So it works fine on my installation... running PHP 5.0.1...Uncaught exception: Uncaught Exception
(I know it's old but I'm using an auto-installer so can't upgrade manually)
I think that might be a cool way to have another level of exception handling, just in case something weird happens.![]()



Thanks for your clarification regarding "finally".![]()
Garcia
Bookmarks