Bali Web Design Studio is a small enthusiast web design studio based in Bali, Jakarta, Indonesia.

PHP Error Exception Handling

Got a question from a friend today who is actually a great desktop programmer and just learn PHP since he believe that the future is in the web (hope you’re right dude). He asked me about how to handling error in PHP. Well i’ll write my answer here so can share with others and got feedback when I was wrong.

In PHP there are few options that you can use to handle errors & exceptions during script executions and as developer you must considering not the easiet but the most flexible approach which will help you when debugging your appllication, the more detail error message provided the faster you can fix it. Flexible means its easy for you to change the application behaviour when unexpected things happened during execution like: display error, logging error or even automatically notice you via email.


Do not use @ error control operator

PHP has magic char @ to use in expression which will ignore any error message that may arised during the expression execution, its bad since you cannot track if some unexpected result returned by the expression.

// error message will be ignored
@include('non_existing_file.php')

Currently the “@” error-control operator prefix will even disable error reporting for critical errors that will terminate script execution. Among other things, this means that if you use “@” to suppress errors from a certain function and either it isn’t available or has been mistyped, the script will die right there with no indication as to why.

Do not use “or die()”

When using “or die()” and error arised the script will stop right there and you will not get any clue except your own message that you provided in die() function. no file, no line, no detail message.

// ' error occured ' with no clue error detail
some_function_call(params_error_trigger) or die('error occured');

Use trigger_error() & set_error_handler()

Benefit of using these combination is that you can get the detail information of the error and with set_error_handler() you can customize the behaviour of your application in handling this error, also if you enabled error logging then you will get this event logged.

// error handling function
function app_error_handler($errno, $errstr, $errfile, $errline) {

if (!(error_reporting() & $errno)) {
// not included in error reporting level
return;
}

switch ($errno) {
case E_USER_ERROR:
// write custom handling for user error level
break;
case E_WARNING
case E_USER_WARNING:
// write custom handling for warning error level
break;
case E_NOTICE
case E_USER_NOTICE:
// write custom handling for notice error level
break;
default:
// write default error level handling
break;
}

if (ini_get("display_errors")) {
// print error
}elseif (ini_get('log_errors')) {
error_log('detail error message');
} else {
// not display nor log
}

return true;
}

set_error_handler('app_error_handler');

// error thrown
some_function_call(params_error_trigger) or trigger_error('yeeehaaa error occured!',E_USER_ERROR);

Use Exception handling & try{} catch() {}

Exception handling is programmer’s’ best friend, its enabled us to create completely custom behaviour for our application to handling unexpected thing that may occured during execution.

try {
// some process
} catch(Exception $e) {
// do exception handling
echo $e->getMessage();
}

We also can get benefit of using exception handling for some error by turn it into exception

set_error_handler('app_error_handler',E_WARNING);
function app_error_handler($errno, $errstr, $errfile, $errline) {
throw new Exception("Error: $errno: $errstr at $errfile line $errline");
}
try {
// some process where error occured
} catch(Exception $e) {
// do exception handling
echo $e->getMessage();
}

References:

  1. PHP Exceptions
  2. PHP set_error_handler()
  3. PHP Error Functions

2 Responses.

  1. Beth August 26, 2010

    Great article on error handling – I’ve just learnt how to use the try {} catch {} method and it has saved a lot of strange headaches in code, especially with GD plugins and thumbnail generators!!

    Great blog, please keep posting x

  2. budiarsa August 18, 2010

    Hi chazzuka, your blog is nice and a lot of great articles and one of them is on
    http://www.chazzuka.com/blog/?p=72 , could you please send me the source code ? I’m now on my school assignment and i want to create file management using jqueryfiletree the problem is i want to separate the directory and the files just like windows explorer

    thanks
    regards
    B

Leave a Reply

bali web design studio Freelance Web Developer Works Bali Web Design Portfolio Get Web Design Quotation RSS Feeds