views:

396

answers:

2

I used to use this when I wanted to trigger errors in PHP, coming from a PHP4 background. Note I had my own set_error_handler() for handling these errors.

if ($error) {
    trigger_error('Sorry, error has occured');
}

I can't remember where, but sometime ago someone told me I should be 'using exceptions'. As I'm re factoring a lot of my old code, I figured now is the time to get some good advice on my error handling implementation.

Now that I'm using PHP5 (and a bit smarter than I was when I wrote the older code), is my trigger_error() just an old way of doing things, and if so, what is the best way to handle errors in PHP5?

+3  A: 

Yes, you may want to start looking into the PHP 5 exception model. Remember though that just because something is new doesn't mean that you must adopt it. Only adopt those features that you need and make sense in your domain.

That being said, I feel that exceptions are a good concept to grasp and even if you decide not to adopt them you will be all the better for the experience.

I would like to suggest that you read PHP: Exceptions - Manual:

PHP 5 has an exception model similar to that of other programming languages. An exception can be thrown, and caught ("catched") within PHP. Code may be surrounded in a try block, to facilitate the catching of potential exceptions. Each try must have at least one corresponding catch block. Multiple catch blocks can be used to catch different classes of exeptions. Normal execution (when no exception is thrown within the try block, or when a catch matching the thrown exception's class is not present) will continue after that last catch block defined in sequence. Exceptions can be thrown (or re-thrown) within a catch block.

I would also encourage you to read What Is an Exception? (Note this is a Java tutorial but the concepts are universal)

When an error occurs within a method, the method creates an object and hands it off to the runtime system. The object, called an exception object, contains information about the error, including its type and the state of the program when the error occurred. Creating an exception object and handing it to the runtime system is called throwing an exception.

Edit: In order to implement a global exception handler (basically in order to establish a default exception handler that will handle previously unhandled exceptions) you will want to us the set_exception_handler function.

Andrew Hare
Is there a general way to catch all exceptions like set_error_handler() handles all errors? Or am I still in the old PHP4 mindset? Thanks for your answer, BTW.
alex
Yes you can do something similar to what you are used to (please see my edit). However I think you may want to read a bit more about exceptions as they are a much more flexible than you seem to think. It is better to only handle exceptions that you understand and can recover from and then use a default exception handler to log unhandled exceptions and then terminate the application.
Andrew Hare
Great answer and edit :) I'll look into these...
alex
+2  A: 

Using exceptions is the object-oriented way to trigger and handle your own application errors.

The PHP manual topic on exceptions is probably a good place to start.

Here is a small example:

function doSomething() {
    if ($error) {
        throw new Exception('Some descriptive error message.');
    }
}

try {
    doSomething();
}
catch (Exception $e) {
    die('<p class="error">' . $e->getMessage() . '</p>');
}
Jon Benedicto
is getMessage() a method that will get the argument of any exception?
alex
Yes, it will get the exception's message: http://php.net/manual/en/exception.getmessage.php
Chad Birch