Error Handling in PHP
Different error handling methods:
Simple "die()" statements Custom errors and error triggers Error reporting
Using the die() function
<?php if(!file_exists("welcome.txt")) { die("File not found"); } else { $file=fopen("welcome.txt","r"); } ?>
Creating a Custom Error Handler
This function must be able to handle a minimum of two parameters (error level and error message) but can accept up to five parameters (optionally: file, line-number, and the error context):
Error_function
error_function (error_level , error_message, error_file , error_line, error_context)
Parameter error_level error_message error_file
Description Required. Specifies the error report level for the userdefined error. Must be a value number. Required. Specifies the error message for the userdefined error Optional. Specifies the filename in which the error occurred
error_line
error_context
Optional. Specifies the line number in which the error occurred
Optional. Specifies an array containing every variable, and their values, in use when the error occurred
Error Report levels
Value 2 8 256 Constant E_WARNING E_NOTICE E_USER_ERROR Description Non-fatal run-time errors. Execution of the script is not halted Run-time notices. The script found something that might be an error, but could also happen when running a script normally Fatal user-generated error. This is like an E_ERROR set by the programmer using the PHP function trigger_error() Non-fatal user-generated warning. This is like an E_WARNING set by the programmer using the PHP function trigger_error() User-generated notice. This is like an E_NOTICE set by the programmer using the PHP function trigger_error() Catchable fatal error. This is like an E_ERROR but can be caught by a user defined handle All errors and warnings
512 1024 4096 8191
E_USER_WARNING E_USER_NOTICE E_RECOVERABLE_ERROR E_ALL
function customError($errno, $errstr) { echo "<b>Error:</b> [$errno] $errstr<br />"; echo "Ending Script"; die(); }
Set Error Handler
set_error_handler("customError");
Example
<?php //error handler function function customError($errno, $errstr) { echo "<b>Error:</b> [$errno] $errstr"; } //set error handler set_error_handler("customError"); //trigger error echo($test); ?>
Trigger an Error
users can trigger errors when an illegal input occurs.
In PHP, this is done by the trigger_error() function.
Example
<?php $test=2; if ($test>1) { trigger_error("Value must be 1 or below"); } ?>
display error messages caused by your PHP script
In code write 1) error_reporting(E_ALL); 2)Another way ,edit your php.ini file and include this option: error_reporting = E_ALL To turn error reporting off for a single document, include this line: error_reporting(0);
Exceptions Handling:
PHP 5 has an exception model similar to that of other programming languages Try - A function using an exception should be in a "try" block. If the exception does not trigger, the code will continue as normal. However if the exception triggers, an exception is "thrown". Throw - This is how you trigger an exception. Each "throw" must have at least one "catch". Catch - - A "catch" block retrieves an exception and creates an object containing the exception information
Example
<?php try { $error = 'Always throw this error'; throw new Exception($error); // Code following an exception is not executed. echo 'Never executed'; } catch (Exception $e) { echo 'Caught exception: ', $e->getMessage(), "\n"; }
// Continue execution echo 'Hello World'; ?>
Exception functions
getMessage()- message of exception getCode() - code of exception getFile() - source filename getLine() - source line getTrace() - n array of the backtrace() getTraceAsString() - formated string of trace
Creating Custom Exception Handler:
string set_exception_handler ( callback $exception_handler )