Php – Best method in PHP for the Error Handling ? Convert all PHP errors (warnings notices etc) to exceptions

exception handlingexceptionsPHP

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 ?

Best Answer

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.

Related Topic