views:

48

answers:

3

I am developing some project. And I want to control different errors. I know that in all popular frameworks and php projects there are different Exceptions. But I think that is not required work. If the error is occured we can make die() with our message. 1. What are the main pluses of Exceptions? 2. Can I control my errors with die()?

Thank you.

+2  A: 

Alexander,

die() and Exceptions accomplish different things.

the "die" language construct just halts the execution of a script and possibly outputs the parameters if it has been called like a function.

On the other hand, exceptions are more advanced structures that are used in OOP contexts to give the developer more flexibility as to whether a script needs to be stopped and if so, in what manner, what output to be shown to the user etc.

Exceptions are a little bit more complex than this so you should perhaps document yourself with some OOP first or for that matter read about zend framework and you'll get a grasp of what Exceptions are.

For simple stuff though, you can always use exit (or die, which is the same thing).

I hope this helps, Slavic

Slavic
+1  A: 

1 What are the main pluses of Exceptions?

The main advantages are:

  • failing functions don't have to pollute their return with error conditions
  • typed exceptions can be handled at appropriate levels in the code, you decide which portion of the code can handle which errors
  • you can store a lot more information about the error condition in the exception itself, making handling it, and possibly recovering from it, easier.

2 Can I control my errors with die()?

I'd hardly call it control, I'd call it giving up on actually handling an error. At no point in my projects is a die() actually user-friendly, and all those die('...some error condition...'); examples of PHP code are IMHO only suited for projects in development. In production, you'll want your users to be able to continue their tasks / programs in the easiest way possible, so a 'try again' (if error condition is not likely to be met again), 'sorry that doesn't work' / other kinds of messages, forms / pages are all more desirable then die().

Wrikken
+2  A: 

Well, you could use die(). But that makes all errors fatal. Meaning that you cannot try to recover from the error at all. In some cases that's fine to do.

But in most cases, you may want the ability to "clean up" after the error, or to try another method. This is where exceptions come in handy... They let you chose where and if you want to handle the error. They let you try to gracefully recover from the errors.

For example, let's say you have a method which downloads a file from a remote server: downloadFromRemoteServer($address);

If you use die(), if the download fails, the script terminates. End of story.

But if you use exceptions, you could try another server or even try a different method (HTTP vs FTP, etc):

try {
    $file = downloadFromRemoteServer('http://example.com/foo');
} catch (DownloadFailedException $e) {
    try {
        $file = downloadFromRemoteServer('http://secondtry.example.com/foo');
    } catch (DownloadFailedException $e2) {
        die('Could not download file');
    }
}
return $file;

But remember that Exceptions are useful only for exceptional circumstances. They are not meant to be used for any possible error. For example, if a user doesn't verify their email address correctly, that's not exceptional. But if you can't connect to the database server, or have a conflict in the DB, that would be an exception circumstance...

ircmaxell