Page 1 of 1

Try...Catch

Posted: Thu Jun 07, 2007 7:47 pm
by JellyFish
Does PHP 4 or 5 have the try and catch statements built in?

Posted: Thu Jun 07, 2007 7:48 pm
by Luke
PHP4 - no
PHP5 - yes

http://us2.php.net/catch

Posted: Thu Jun 07, 2007 7:50 pm
by JellyFish
Okay, I have v4... What's the alternative to them?

Posted: Thu Jun 07, 2007 8:03 pm
by bdlang
There really isn't an alternative, unless you want to use if..else..then blocks to control the script flow based on conditions.

Posted: Thu Jun 07, 2007 8:07 pm
by Luke
you might talk to d11wtq. Here is a hackaround (his word) he came up with for his Swift library:

http://swiftmailer.org/wikidocs/v3/misc/php4errors

Posted: Thu Jun 07, 2007 8:29 pm
by JellyFish
I guess with a statement with an include() function I could do:

Code: Select all

inlcude($something) || die("Error");

Posted: Thu Jun 07, 2007 9:08 pm
by Benjamin
One way to emulate try/catch is to use a switch. You have to add additional code to customize the behavior you want.

Code: Select all

switch (null)
{
    default:
        if ($x === false)
        {
            $error = 'blah';
            break;
        }
}

Posted: Fri Jun 08, 2007 1:33 am
by Chris Corbyn
Believe me, I've had all the headaches on this and tried numerous workarounds. It's just not doable. There's no way PHP4 will ever reach that level of bubbling that try/catch provides. The beauty of exceptions is that the exception my be thrown at a very low level where you're not able to monitor it, but execution will still jump directly from your try to your catch block.

I'd suggest using trigger_error() and set_error_handler() but it's in no way shape or form the same.

It really depends on how you use exceptions. Exceptions provide two things:

* Error messages
* Program flow

The latter is where PHP4 falls flat on its face.

Posted: Fri Jun 08, 2007 3:00 am
by JellyFish
Well, I dont' really need to throw exceptions but just check if a statement returns an error or warning, whichever the include function returns. How would I try an include statement and if it returns an error of some kind, kill the program with exit or die?

Posted: Fri Jun 08, 2007 7:30 am
by Chris Corbyn
set_error_handler()