how to handle exceptions in php4(please give example also)

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
rajan
Forum Contributor
Posts: 110
Joined: Sun Aug 28, 2005 7:42 pm
Location: Lucknow, UP, India

how to handle exceptions in php4(please give example also)

Post by rajan »

how to handle exceptions in class just by creating the instance on php4. please give me answer with some coding examples.
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post by foobar »

To quote myself from this thread:
foobar wrote: There is no exception handling in PHP4.
Also, I think I explained the alternatives in said thread pretty well. Might want to get back to it.
User avatar
rajan
Forum Contributor
Posts: 110
Joined: Sun Aug 28, 2005 7:42 pm
Location: Lucknow, UP, India

Post by rajan »

but how to handle the error in class ? how to get the error messages or u can say user generated error messages from inside class?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

rajan wrote:but how to handle the error in class ? how to get the error messages or u can say user generated error messages from inside class?
In PHP4 you can't catch the errors.

You can surpress the output and use your own error output by using the @ syntax.

Code: Select all

if ($foo = @some_undefined_function()) echo $foo;
else echo 'WTF?? Function is undefined';
The @ simply stops the error from being displayed and simply returns false instead.

You can also log the errors to a log file rather than displaying them on screen. As ~foobar says... exception handling is new to PHP5.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

::Agree with Jenk::

There really isn't any satisfactory way in PHP4 to catch errors: you'll probably end up bubbling them up to a caller that knows enough how to handle them.

Also, PEAR has exception emulation: you may want to take a look at that.
Post Reply