User can set his own exception handler for complete application and also for all uncaught exception. |
Errors are the part of every programming language and normally programmers don’t want to pay much attention to handling the errors. PHP of course by default handle the errors and maintain logs also, but we should handle all the errors. PHP provides better way to create of your own error function.
What type of errors are there in php 1. Parse Errors – compiler error 2. Fatal Errors – execution halts 3. Warnings – execution continues 4. Notices – execution continues php.ini directives to control errors There are 3 important directives in php.ini. display_errors:: keep it off always on production server. log_errors:: keep it on will log errors in server’s logs error_reporting:: error reporting level setting can be done e.g.E_ALL~E_NOTICE Examples for creating a custom error handler in php |
ini_set('display_errors', 'Off'); ini_set('log_errors', 'On'); function UserErrorHandler($errno, $errstr, $errfile, $errline) { switch ($errno) { case E_USER_WARNING: echo "USER WARNING : [$errno] $errstr\n"; break; case E_USER_NOTICE: echo "USER NOTICE : [$errno] $errstr\n"; break; case E_USER_ERROR: echo "USER ERROR : [$errno] $errstr\n"; echo "There is a fatal error on line no $errline in script $errfile\n"; echo PHP_VERSION . " on (" . PHP_OS . ")\n"; echo "Execution ends\n"; exit(); break; default: echo "Unknown error types: [$errno] $errstr\n"; break; } /* you can send email to admin or any other logging system */ return true; } set_error_handler("UserErrorHandler",E_ALL); //register the custom error function
Explanation
The function which is used to create a custom error handler is set_error_handler(). In php5 it accept 2 parameters, error handler function name and error type which are handled by that function. In older versions of php this function was accepting only one parameter.
Error handling function : ‘UserErrorHandler’ is our custom error handling function. This function will get executed whenever there is any error in code. Then its upto the programmer how to handle the errors, he can mail them to administrator or keep them in some file log or display it to users.complete freedom to manage the errors in system. This function takes 5 parameters as follows
error_level : e.g. E_ALL , E_NOTICE .. error_message : error message error_file : file name in which the error occurred error_line : line no in which the error occurred
error_context : array of variables in that error context
The way of handling errors is different. We can implement the custom error handler in php with help of php5 default class Exception.
We can also set a exception handler for complete application. That mean a function which can handle all the uncaught exceptions. Just look at following code.
/ how to handle error any where in application function uncaughtexception(Exception $e) { echo $e->getMessage()."\n"; echo $e->getCode()."\n"; echo $e->getLine(); } set_exception_handler('uncaughtexception'); throw new exception('The Error is caught');