Throw multiple exceptions

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Throw multiple exceptions

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Throw multiple exceptions

Post 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...
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Re: Throw multiple exceptions

Post 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.
piccoloprincipe
Forum Newbie
Posts: 12
Joined: Wed Jan 16, 2008 4:11 pm

Re: Throw multiple exceptions

Post by piccoloprincipe »

I thinked: define class MultipleException with __construct(array $exceptions) and throw that.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Throw multiple exceptions

Post by Ollie Saunders »

A failed validation is not good grounds for throwing an exception. Can't you just return false?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Re: Throw multiple exceptions

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Throw multiple exceptions

Post by Ollie Saunders »

Oh ok. Can't you just extend the exception class to handle an array of stuff.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: Throw multiple exceptions

Post by Jenk »

ole wrote:A failed validation is not good grounds for throwing an exception. Can't you just return false?
Says who?!
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Throw multiple exceptions

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Throw multiple exceptions

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Throw multiple exceptions

Post 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.
(#10850)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: Throw multiple exceptions

Post 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.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Re: Throw multiple exceptions

Post 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.)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Throw multiple exceptions

Post by Ollie Saunders »

It almost sounds like we agree. 8O
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Re: Throw multiple exceptions

Post by Ambush Commander »

What's wrong with that? ;-)
Post Reply