php OOP Exceptions or die()?

↘锁芯ラ 提交于 2019-12-06 08:47:31

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...

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

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().

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!