Using Custom Error Handler in PHP


The second approach of error handling in PHP is defining custom error handler function.

Syntax:
Function Custom_Function_Name (Level, Message, FileName, LineNumber, Context)
{
// Error Handling Code
}
Here,
Type:
Indicates Type of Error.
Message: Indicates detailed error description.
FileName: Indicates Name of the file in which error generated. It is optional argument.
LineNumber: Indicates number of line in the file where error is generated. It is optional argument.
Context: Indicates an array which contains all the variable along with its value that is in use when an error is generated. It is optional argument.

     

Once you define custom error handler function then you need to set custom error handler using set_error_handler () function. The general syntax of set_error_handler () function is given below:
set_error_handler (Custom_Function_Name);
Once you set custom error handler in your script. Whenever any error generated in your code it will display custom error message.
Consider Following Example:

In the above script you are attempting to divide Number1 by Number2 but the value of Number2 is 0 so it will generate Divide by Zero error message using Custom Error Handler.
The advantage of Custom Error Handler is that it will display custom error message and the execution of the script is not stopped as shown below:

Download Projects


Download Programs