only for RuBoard - do not distribute or recompile Previous Section Next Section

Handling Errors Gracefully

If you come from a C++ or Java background, you might miss exception handling when you use PHP. Exceptions allow functions to signal that an error has occurred and leave dealing with the error to an exception handler. Although PHP does not have exceptions, PHP 4.0.1 introduced a mechanism that can be used in a similar way.

You have already seen that you can trigger your own errors. You can also provide your own error handlers to catch errors.

The function set_error_handler() lets you provide a function to be called when user level errors, warnings, and notices occur. You call set_error_handler() with the name of the function you want to use as your error handler.

Your error handling function must take two parameters; an error type and an error message. Based on these two variables, your function can decide how to handle the error. The error type must be one of the defined error type constants. The error message is a descriptive string.

A call to set_error_handler() will look like this:

					
set_error_handler("myErrorHandler");

				

Having told PHP to use a function called myErrorHandler(), we must then provide a function with that name. This function must have the following prototype:

					
myErrorHandler(int error_type, string error_msg)

				

but what it actually does is up to you.

Logical actions might include

Listing 23.2 contains a script that declares an error handler, sets the error handler using set_error_handler(), and then generates some errors.

Listing 23.2 handle.php—This Script Declares a Custom Error Handler and Generates Different Errors
<?
  // The error handler function
function myErrorHandler ($errno, $errstr)
  {
    echo "<br><table bgcolor = '#cccccc'><tr><td>
          <P><B>ERROR:</B> $errstr
          <P>Please try again, or contact us and tell us that
          the error occurred in line ".__LINE__." of file '".__FILE__."'";
    if ($errno == E_USER_ERROR||$errno == E_ERROR)
    {
      echo "<P>This error was fatal, program ending";
      echo "</td></tr></table>";
      //close open resources, include page footer, etc
      exit;
    }
    echo "</td></tr></table>";
  }
  // Set the error handler
  set_error_handler("myErrorHandler");


  //trigger different levels of error
  trigger_error("Trigger function called", E_USER_NOTICE);
  fopen("nofile", "r");
  trigger_error("This computer is beige", E_USER_WARNING); 
  include ("nofile");
  trigger_error("This computer will self destruct in 15 seconds",
    E_USER_ERROR);
?>

The output from this script is shown in Figure 23.1.

Figure 23.1. You can give friendlier error messages than PHP if you use your own error handler.
graphics/23fig01.gif

This custom error handler does not do any more than the default behavior. Because this code is written by you, you can make it do anything. It gives you a choice about what to tell your visitors when something goes wrong and how to present that information so that it fits the rest of the site. More importantly, it gives you flexibility to decide what happens. Should the script continue? Should a message be logged or displayed? Should tech support be alerted automatically?

It is important to note that your error handler will not have the responsibility for dealing with all error types. Some errors, such as parse errors and fatal runtime errors will still trigger the default behavior. If this concerns you, make sure that you check parameters carefully before passing them to a function that can generate fatal errors and trigger your own E_USER_ERROR level error if your parameters are going to cause failure.

only for RuBoard - do not distribute or recompile Previous Section Next Section