Error Handling

What do you mean by PHP Error Handling?

PHP catches the errors raised by our program and takes appropriate action.It is very simple to handel error in PHP. There are several ways to handle error in PHP,

Using die() function:

While writing PHP program a thorough check for all possible error condition should be made before going ahead and an appropriate action must be taken when required.

Syntax:
<?php
 die("string");
?>
Example:
PHP Script (error_handling.php)
<!DOCTYPE html>
<html>
<head>
    <title>CP Tutorials</title>
</head>
<body>
<h1>Error handling by die() method.</h1>
<?php
$file_name = "new_file.txt";
if(!file_exists($file_name))
{
    die("File not found");
}
else
{
    $file=fopen($file_name,"r");
    print "Opend file sucessfully";
}
?>
</body>
</html>

Custom Error Handling Function:

You can also define your own custom error handling functions in PHP. PHP provides you framework to define error handling functions. Minimum of two parameters handling functions (error level and error message)  can be made, but PHP can accept upto five parameters(file, line-number, and the error context).

 

 Parameter

Description

 error_level

Required - Specifies the error report level for the user-defined error. Must be a value number.

 error_message

Required - Specifies the error message for the user-defined error

 error_file

Optional - Specifies the filename in which the error occurred

 error_line

Optional - Specifies the line number in which the error occurred

 error_context

Optional - Specifies an array containing every variable and their values in use when the error occurred

 

Example:
PHP Script (cosom_error.php)
<?php
//error handler function
function customErr($errno, $errstr) {
    echo "<b>Error:</b> [$errno] $errstr";
}
//set error handler
set_error_handler("customErr");
//trigger error
echo($check);
?>

 

Exception Handling:

In PHP 5 an exception model is introduced similar to other programming language. Exception provides better control over error handling.There are three keywords related to exception,

  1. Try: A function using an exception should be in “try” block and if the exception triggers, an exception is “thrown” and if not program runs normally.
  2. Throw: Throw helps triggering an exception. One “catch” per each “Throw” is required.
  3. Catch: A “catch” block retrieves an exception and creates an object containing the exception information.
Example:
PHP Script (exception_handling.php)
<!DOCTYPE html>
<html>
<head>
    <title>CP Tutorials</title>
</head>
<body>
<h1>Error handling by try-catch method.</h1>
<?php
//create function with an exception
function divide($a = NULL,$b = NULL) {
    $c = $a/$b;
    if($c>5){
        throw new Exception("Output is greater than 5.");
    }
    return true;
}
//trigger exception in a "try" block
try {
    divide(20,5);
    //If the exception is thrown, this text will not be shown
    echo 'Output is less than 5.';
}

//catch exception
catch(Exception $e) {
    echo 'Message: ' .$e->getMessage();
}
?>
</body>
</html>