Throw multiple exceptions
Moderator: General Moderators
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
Throw multiple exceptions
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.
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.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Re: Throw multiple exceptions
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...
EDIT | Bitmask would refer to some class constants...
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
Re: Throw multiple exceptions
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.
-
piccoloprincipe
- Forum Newbie
- Posts: 12
- Joined: Wed Jan 16, 2008 4:11 pm
Re: Throw multiple exceptions
I thinked: define class MultipleException with __construct(array $exceptions) and throw that.
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Re: Throw multiple exceptions
A failed validation is not good grounds for throwing an exception. Can't you just return false?
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
Re: Throw multiple exceptions
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.
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Re: Throw multiple exceptions
Oh ok. Can't you just extend the exception class to handle an array of stuff.
Re: Throw multiple exceptions
Says who?!ole wrote:A failed validation is not good grounds for throwing an exception. Can't you just return false?
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Re: Throw multiple exceptions
*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.
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Re: Throw multiple exceptions
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.
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.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Throw multiple exceptions
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.Chris Corbyn wrote:The whole "Exceptions vs Exceptional situations" debate is getting old very quickly.
(#10850)
Re: Throw multiple exceptions
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.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.
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.
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
Re: Throw multiple exceptions
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.)
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.)
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Re: Throw multiple exceptions
It almost sounds like we agree. 
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
Re: Throw multiple exceptions
What's wrong with that? 