Forum Moderators: coopster

Message Too Old, No Replies

Best practices for exception handling, warnings

Php5

         

heisters

3:22 pm on Jun 1, 2006 (gmt 0)

10+ Year Member



So, what's the best way to be dealing with exceptions? I was taught mostly to just do stuff like


$foo = do_something() or die ("Couldn't do something");

If one throws an exception, should it always be caught, or is something like this okay?:


if (!$foo) {
throw new Exception ("Damn!");
}

Finally, how do you throw non-fatal errors? PHP is always belching up Warnings, but I can't find a warn() or similar function anywhere in the PHP docs/Google.

Thanks!

heisters

3:28 pm on Jun 1, 2006 (gmt 0)

10+ Year Member



ooohhh, answering one of my own questions:


trigger_error ("Oops", E_USER_WARNING);

But still, when's it best to use trigger_error/die and when's it best to use try/catch?

coopster

4:26 pm on Jun 1, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



In a development environment I dump all the errors to the screen until I am happy with testing and feel the code is ready for production. On the production box, absolutely no errors are allowed to be displayed but they are all logged. You can monitor for errors in the logic of your code where you might expect to see something and handle accordingly. For example, if an SQL statement fails you may want to send it to your error_log and perhaps email or text message your cell phone.