Try...Catch

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Try...Catch

Post by JellyFish »

Does PHP 4 or 5 have the try and catch statements built in?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

PHP4 - no
PHP5 - yes

http://us2.php.net/catch
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

Okay, I have v4... What's the alternative to them?
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post 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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

I guess with a statement with an include() function I could do:

Code: Select all

inlcude($something) || die("Error");
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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;
        }
}
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post 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?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

set_error_handler()
Post Reply