Page 1 of 2

Throw multiple exceptions

Posted: Tue Mar 04, 2008 11:35 pm
by Ambush Commander
I'm curious to know how one would do that. The reason why I'd like to know this is I'm implementing an "or" validator, which means that if one of them passes, we swallow everything, but if all of the sub-validators throw exceptions, the validator needs to throw an exception. Like $status = doThis() || doThat().

Right now I'm shoving the string representation of each of the exceptions into one Exception and throwing that. There probably is a more elegant way, though.

Re: Throw multiple exceptions

Posted: Wed Mar 05, 2008 1:21 am
by Chris Corbyn
Can you throw a particular type of Exception which exposes a new method that returns a bitmask?

EDIT | Bitmask would refer to some class constants...

Re: Throw multiple exceptions

Posted: Wed Mar 05, 2008 8:11 am
by Ambush Commander
No. And here's why: most of my exceptions only differ in stack-trace and error message, so I'd have to define a bit-mask for every error I want to throw.

Re: Throw multiple exceptions

Posted: Fri Mar 07, 2008 8:03 am
by piccoloprincipe
I thinked: define class MultipleException with __construct(array $exceptions) and throw that.

Re: Throw multiple exceptions

Posted: Sun Mar 09, 2008 2:05 pm
by Ollie Saunders
A failed validation is not good grounds for throwing an exception. Can't you just return false?

Re: Throw multiple exceptions

Posted: Sun Mar 09, 2008 4:24 pm
by Ambush Commander
Well, this is a special sort of validation :-) Developers can specify meta-data for how the class should work, so I'm validating their data and should fatally error-out if there's problems.

Re: Throw multiple exceptions

Posted: Sun Mar 09, 2008 4:37 pm
by Ollie Saunders
Oh ok. Can't you just extend the exception class to handle an array of stuff.

Re: Throw multiple exceptions

Posted: Sun Mar 09, 2008 6:17 pm
by Jenk
ole wrote:A failed validation is not good grounds for throwing an exception. Can't you just return false?
Says who?!

Re: Throw multiple exceptions

Posted: Sun Mar 09, 2008 8:55 pm
by Chris Corbyn
*yawn* we should just rename the base Exception class to "Throwable" and be done with it. The whole "Exceptions vs Exceptional situations" debate is getting old very quickly.

Re: Throw multiple exceptions

Posted: Sun Mar 09, 2008 9:32 pm
by Ollie Saunders
I don't think the issue is what you call it. The issue is "do I want my program to fatal if this happens?". I know you can catch exceptions but do you really want to bother and risk your program dying if you forget to?

Anyway as AC explained these aren't actually failed validations we are talking about, this is incorrect use of an interface which is good grounds for dying - you should fail fast.

Re: Throw multiple exceptions

Posted: Sun Mar 09, 2008 10:15 pm
by Christopher
Chris Corbyn wrote:The whole "Exceptions vs Exceptional situations" debate is getting old very quickly.
While it is getting old, I don't think we have resolved it in a way where the majority of us generally agree on a set of guidelines.

Re: Throw multiple exceptions

Posted: Sun Mar 09, 2008 10:40 pm
by Jenk
Chris Corbyn wrote:*yawn* we should just rename the base Exception class to "Throwable" and be done with it. The whole "Exceptions vs Exceptional situations" debate is getting old very quickly.
Whilst I'm not wanting to argue or the like, that's showing the biggest misunderstanding of all - of what an exception (i.e. the very definition of the word) is.

An exception is something that happens outside of the ordinary or expected scope/behaviour/whatever. In this example, if the method is expecting valid data and receives invalid data, it's an exception to what is expected - it did not expect to receive invalid data.

Re: Throw multiple exceptions

Posted: Sun Mar 09, 2008 10:45 pm
by Ambush Commander
Here's my take on Exception (and it ties to Throwable): it's a convenient way of implementing gotos in a non-messy manner to break out of the function stack with some information. Accordingly, you should use it when you need to quickly get out of several functions quickly, when a function does not have a range of "invalid" data (i.e. it can return null or false validly), or when you need to transmit some extra information when exiting (the alternative would have been to return some sort of Error object).

If we take performance considerations into effect, users should avoid using Exceptions in code that will be run frequently.

That's about it. Exceptions are a tool; use them when they make your life easier, don't use them when they don't. Validators, for example, don't usually need to break out of a function stack and have an easily defined set of invalid values, but often do need to transmit extra information (traditionally, this is implemented with a getError() method.)

Re: Throw multiple exceptions

Posted: Sun Mar 09, 2008 10:50 pm
by Ollie Saunders
It almost sounds like we agree. 8O

Re: Throw multiple exceptions

Posted: Sun Mar 09, 2008 10:52 pm
by Ambush Commander
What's wrong with that? ;-)