PHP Custom Error Handling: Part 1

June 24th, 2005

PHP has many built in error reporting levels, sometimes it is better to use these when your application fails on a fundamental level, specifically E_USER_ERROR.

Sometimes instead of handling fatal errors where they happen, I find bailing out into a global error handler more efficient. For instance take this concept example:

// This could be a failed
MySQL update or whatever
if ('foo' == 'foo')
{
       die('foo really shouldn't equal foo');
}
// This could be displaying the failed update
echo 'this doesn't work if foo != foo';

Ok, so far so good, it may be poor man’s error handling, but at least it gets the job done. The user will never see the echo of the string that relies on foo!=foo, or the “failed update”.

It’s a hassle to continually code die() error messages, especially if you want to render the errors with pretty HTML; and there are clear benefits as to why you should do this:

I am sorry, but the page you were looking for has been moved. Here is a list of search results applying to the page you wanted:
-etc-
Use the search box below if you want to continue searching.
-etc-

In Part 2 I’ll show you techniques I use when creating useful errors similar to the above. That’s when the real code will start.

Comments are closed.