Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

What is the best method in PHP for the Error Handling ?

is there a way in PHP to Convert all PHP errors (warnings notices etc) to exceptions ?

what the best way/practise to error handling ?

again:

if we overuse exceptions (i.e. try/catch) in many situations, i think application will be halted unnecessary.

for a simple error checking we can use return false; but it may be cluttering the coding with many if else conditions.

what do you guys suggest ?

share|improve this question
2  
Step 1 would be to not write code that issues warnings, notices, etc. –  sevenseacat Oct 2 '12 at 7:31

3 Answers 3

If your development and production environments are separate, you should have the display_errors ini setting set to off on production, and on (and I would set the error_reporting level to -1: all errors) on development. It shouldn't be too difficult to write code that doesn't emit any warnings or notices through normal usage unless you're using a PEAR library, as those have a lot of legacy code that causes warnings.

As for errors and exceptions, on development you should just treat them normally so you can actually see what's wrong, and on production you can use the set_error_handler and set_exception_handler methods to catch errors and exceptions and display an error page to the user instead and write the actual error to a log file that you can check later.

Other than that, I wouldn't use any special tactics for error suppression.

share|improve this answer

Short Answer: If you use PHP 5, you can handle error with exceptions:

http://fr2.php.net/manual/en/class.exception.php

This way is cleaner than manual set exception message, because you have access to a try catch system and you can isolate exception handling

share|improve this answer

When you develop your code you should have all the error reportings in PHP enabled, including notices and warnings. Your production code should never produce a PHP Error. In some special cases you may survive with some PHP Warnings and Notices, but on your production server you should only show the Errors to the user.

Next, PHP Errors mean that you have something really messed up in your code and such things should never reach production. Any amount of decent automated tests will make sure this never happens. => Correction for clarity: "... will make sure this happens rarely."

I don't recommend you to transform PHP Errors, Warnings and Notices into Exceptions or "FALSE". Never! If they happen on your computer the application has a serious problem, so don't transform the error into something that you can catch and than forget / ignore easily.

When you have situations where your code can go wrong, use proper verification and throw exceptions. Even more, whenever possible make your own exception and throw the one most appropriate for that situation.

For example, instead of letting PHP throw an "Invalid index" when you try to read an element of an array, you may do something like:

if (!isset($superWeapons['nuclear'])) 
  throw new MissingSuperweaponException('We heave no nuclear weapons awailable!');

Than, somewhere at a higher level of your application you can catch all these exceptions and show the user the nice messages you want.

Please note, that exceptions are for exceptional cases, for situations in which your application should never be. Use then only when needed.

When there is a result of an operation, a result which can be TRUE or FALSE or a value or a string, that is not an exceptional case, that is a result or a return value. For example, when you do a search for a string the result can be the index of the searched text. However, not finding the text is not an exceptional case, you should just return zero or false, not an exception.

share|improve this answer
2  
"Any amount of decent automated tests will make sure this never happens." - I think you're living in a fantasy world.. –  Explosion Pills Oct 2 '12 at 9:23
    
I intentionally didn't specify exact amount because each person will heave it's own view on this subject. I am not leaving in a fantasy world... and still I am seeing PHP Errors in production very rarely... –  Patkos Csaba Oct 2 '12 at 9:27
    
I agree that exceptions are for exceptional cases, but anything is better than having a method that returns either an index or false! I do not know how many bugs are introduced by intentionally abusing dynamic typing. The fact that you do not have mark the type of a returned value does not mean that you should mix types freely. –  Andrea Oct 2 '12 at 12:06
    
@Andrea: Yeah, I know. But even if I don't agree with the ways PHP works I am still sometimes suggesting in examples uses cases like the one above because that's how many PHP functions work and my answer can be related to the language. But the correctness of PHP is a totally different story. Thanks though for pointing out this problem. –  Patkos Csaba Oct 2 '12 at 13:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.