开发者

Store PHP Exceptions in array

I am really not sure if this is the right way to go, since exceptions is really a fresh subject to me. Is it possible to catch multiple exceptions (let the execution of the script continue) and then store the exceptions in a array to be able to return all exceptions caused?

By开发者_高级运维 that said, it would be awesome to be able to use exceptions to more than just showing an error that kills the application (script)

Thanks!


At first, exception handling is not as trivial as it looks like, so you should invest a little time in this. :-)

You should look at exceptions explicit as an error you can't handle in the current code/function. If you can solve the problem, there is no need to throw and handle an exception.
Don't use it as mechanism to handle expected behavior.

Sure it is possible to catch multiple exceptions, continue the code execution and store them in an array, but it doesn't make sense. You through an exception in your code if you really encounter an error you cannot deal with in your current code (for example suddenly closed sockets, etc.). The rule then is:
Only catch an exception if you can do something useful with it or throw another exception

For tracking errors in your application you should use other techniques than storing them in an array and retrieving them later. Use Logging (there are excellent frameworks for example Log4PHP) to document minor application errors and warnings.

By that said, it would be awesome to be able to use exceptions to more than just showing an error that kills the application (script)

An exception should kill the application only in the case there is nothing you can do about it. Also in most cases it is a good idea to catch all exceptions on the highest level in your script, log the error with a stack trace and present the user a nice error message instead of just "kill" everything. :-)

For just some syntax examples see W3Schools PHP Exception Handling. A larger article about this topic is posted on Devshed.


This isn't really what exceptions are for. In many languages, exceptions are simply objects that you could catch and simply shove into an array for later examination, but it's really probbly a bad design.

The very name of the mechanism indicates that something "exceptional" has happened that you need to process immediately.


You can do more with them than just killing the script - but San Jacinto is right in saying that it wouldn't be particularly good practice to store them in an array to process later.

Maybe you should have a read of this (including the examples, they'll be useful):

http://php.net/manual/en/language.exceptions.php

That should show you some other ways you can use exceptions rather than just stopping execution.

Good luck!


Larry Wall writes about this in his last State of the Onion article. Everyone wants to be able to have an error throwing/catching framework. Truth is, it becomes bad coding practice to just rely on the framework to handle one's poorly-checked code. Plus, it makes it really hard to debug.

My advice would be to, instead of something like this:

try {
    $fh = fopen("foo.txt", 'r');
    if (!$fh) {
       throw new Exception("foo.txt not found");
    }
    # ...
} catch (Exception $e) {
    # report errors
}

Just collect them in a buffer of messages as they happen:

@errors = array();
if (! (is_file("foo.txt") && $fh = fopen("foo.txt", 'r')) ) {
    $errors []= "foo.txt not found";
}
# ...

That way, your stack pointer isn't jumping all over the place trying to find a handler for the Exception.

PHP tries to be too much like Java, IMHO.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜