
PHP ─ Error and Exception HandlingError handling is the process of catching errors raised by your program and then taking appropriate action. If you would handle errors properly, then it may lead to many unforeseen consequences. It is very simple in PHP to handle errors. Using die() function While writing your PHP program you should check all possible error condition before going ahead and take appropriate action when required. Try the following example without having /tmp/test.xt file and with this file.
<?php
if(!file_exists("/tmp/test.txt")) { die("File not found"); } else { $file=fopen("/tmp/test.txt","r"); print "Opend file sucessfully"; } // Test of the code here. ?> You can thus write an efficient code. Using the above technique, you can stop your program whenever it errors out and display more meaningful and user-friendly message. Defining Custom Error Handling Function You can write your own function to handling any error. PHP provides you a framework to define error-handling function. 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): Syntax
error_function(error_level,error_message, error_file,error_line,error_context);
These error report levels are the different types of error the user-defined error handler can be used for. These values cab used in combination using | operator
int error_reporting ( [int $level] )
Here is how you can create an error handling function:
<?php
function handleError($errno, $errstr,$error_file,$error_line) { echo "<b>Error:</b> [$errno] $errstr - $error_file:$error_line"; echo "<br />"; echo "Terminating PHP Script"; die(); } ?> Once you define your custom error handler, you need to set it using PHP built-in library set_error_handler function. Now let’s examine our example by calling a function which does not exist.
<?php
error_reporting( E_ERROR ); function handleError($errno, $errstr,$error_file,$error_line) { echo "<b>Error:</b> [$errno] $errstr - $error_file:$error_line"; echo "<br />"; echo "Terminating PHP Script"; die(); } //set error handler set_error_handler("handleError"); //trigger error myFunction(); ?> Exceptions Handling PHP 5 has an exception model similar to that of other programming languages. Exceptions are important and provides a better control over error handling. Let’s now explain the new keyword related to exceptions.
Copy and paste the following piece of code into a file and verify the result.
<?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'; ?> In the above example, $e->getMessage function is used to get error message. The following functions can be used from Exception class.
You can define your own custom exception handler. Use the following function to set a user-defined exception handler function.
string set_exception_handler ( callback $exception_handler )
Here exception_handler is the name of the function to be called when an uncaught exception occurs. This function must be defined before calling set_exception_handler(). Example
<?php
function exception_handler($exception) { echo "Uncaught exception: " , $exception->getMessage(), "\n"; } set_exception_handler('exception_handler'); throw new Exception('Uncaught Exception'); echo "Not Executed\n"; ?> Check the complete set of error handling functions at PHP Error Handling Functions. |