views:

156

answers:

4

Hi everyone,

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!

+5  A: 

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.

San Jacinto
+2  A: 

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!

Gary
+1  A: 

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.

amphetamachine
Any moderately complex application would become exponentially complex with this method, unless of course you are referring to exceptions that really aren't that bad and should actually be warnings. Caching (not "catching", but "caching") )them is easy; it's dealing with them later that is very difficult. Part of the problem is that you lose much of the state that you had when the exception happened. Another problem is the issue of WHEN to process these exceptions. I personally think your method opens a can of worms.
San Jacinto
+3  A: 

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.

echox
Hi Echox! Thanks for a great and extensive reply. Do you have any examples of how to use Log4PHP to an array?Thanks!
Industrial
Why would you want to store everything in an array? :-) Just log it to your filesystem or a database table?But if you really need to store your "log" into an array simply use $e->getMessage(); and store the result string in an array. See Multiple Exceptions and Rethrowing Exceptions in the W3Schools Examples.
echox
Hi again! I am trying to collect a number of errors/status messages created from a chained action to send back to the user together with the result of the function (true/false)
Industrial
With that said, it doesnt feel to good to collect a error about the database connection is lost, through the database :)
Industrial
@Industrial, what you are asking now is completely different that what you asked in the first place. Storing the exceptions is very different than storing the error message.
San Jacinto