Error Handling In Php Best Practices
Contents |
the NYPHP flickr Group. Link up with NYPHP at the PHP LinkedIn Group. About NYPHP » Charter » Mission » What is NYPHP? » Principals » Sponsors PHP Error Handling NYPHP - PHundamentals PROBLEM: Every well-constructed PHP application should have error php trigger error handling. While there is no definitive method for handling errors since it varies depending on
Php Exception Handling Best Practices
application needs and a developer's style, nonetheless, there are some "best practices" that should be implemented in all PHP applications. WHAT IS AN
Php Error Handling Class
ERROR? The answer may seem straightforward but actually requires a bit of discussion. An "error" is an expected or unexpected event that occurs when your PHP code is running. An "expected" error can be a database query that returns
Php Set Error Handler
no result or an html form that is missing values for designated required fields. An "unexpected" error is one that assumes a particular application state which, for some as yet unknown reason, does not exist. The most obvious unexpected error is a database that is not running or a missing file that one of your scripts expects to find. ERROR HANDLING ENVIRONMENT The recommended "best practice" has three parts: The developer should be alerted to all errors, error handling best practices c# notices, warnings, etc. both during development and when the PHP application goes into production. The developer should be able to choose how to be notified of these problems. No errors, notices, warnings, etc. should ever be displayed to the user. Even in development, errors dumped to the browser can be bad because they can become hidden in the HTML. In production, you should display a generic page that says "System down for maintenance" or some other generic message. At no time do you want to tip off a hacker as to what went wrong, nor do you want to display the details of what went wrong. That information is for you alone. Although some developers feel that errors should be displayed during development and even in a production environment - modified, of course, so as not to tip off a hacker - we will assume here that errors will never be displayed. We can set these three parameters in one of two ways. If you have control over the php.ini file then set the following parameters: (See Note 1) error_reporting = E_ALL
log_errors = On
display_errors = Off
If you don't have control over php.ini, then at the beginning of your script you can set the same parameters using the following: error_reporting(E_ALL);
http://nyphp.org/PHundamentals/7_PHP-Error-Handling Sample File (includingDocblock Commentstandards) The PEAR toolbox Error Handling Guidelines This part of the Coding Standards describes how errors are handled in PEAR packages that are developed for PHP 5 and 6. It uses Exceptions, introduced in PHP 5.0 https://pear.php.net/manual/en/standards.errors.php with Zend Engine 2, as the error handling mechanism. Definition of an error An error is defined as an unexpected, invalid program state from which it is impossible to recover. For the sake of definition, recovery scope is defined as the method scope. Incomplete recovery is considered a recovery. One pretty straightforward example for an error /*
*ConnecttoSpecifiedDatabase
*
*@throwsExample_Datasource_Exceptionwhenitcan'tconnect
*tospecifiedDSN.
*/
functionconnectDB($dsn)
{
$this->db=&DB::connect($dsn);
if(DB::isError($thishttp://stackoverflow.com/questions/5551668/what-are-the-best-practices-for-catching-and-re-throwing-exceptions about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up What are the best practices for catching and re-throwing exceptions? up vote 71 down vote favorite 50 Should caught exceptions be re-thrown directly, or should they be error handling wrapped around a new exception? That is, should I do this: try { $connect = new CONNECT($db, $user, $password, $driver, $host); } catch (Exception $e) { throw $e; } or this: try { $connect = new CONNECT($db, $user, $password, $driver, $host); } catch (Exception $e) { throw new Exception("Exception Message", 1, $e); } If your answer is to throw directly please suggest the use of exception chaining, I am not able to understand a real world scenario handling best practices where we use exception chaining. php exception share|improve this question edited Nov 15 '12 at 16:04 Jon 272k40474594 asked Apr 5 '11 at 12:18 Rahul Prasad 3,21842638 add a comment| 5 Answers 5 active oldest votes up vote 144 down vote accepted You should not be catching the exception unless you intend to do something meaningful. "Something meaningful" might be one of these: Handling the exception The most obvious meaningful action is to handle the exception, e.g. by displaying an error message and aborting the operation: try { $connect = new CONNECT($db, $user, $password, $driver, $host); } catch (Exception $e) { echo "Error while connecting to database!"; die; } Logging or partial cleanup Sometimes you do not know how to properly handle an exception inside a specific context; perhaps you lack information about the "big picture", but you do want to log the failure as close to the point where it happened as possible. In this case, you may want to catch, log, and re-throw: try { $connect = new CONNECT($db, $user, $password, $driver, $host); } catch (Exception $e) { logException($e); // does something throw $e; } A related scenario is where you are in the right place to perform some cleanup for the failed operation, but not to decide how the failure should be handled at the top level. In earlier PHP versions this would be impleme