Page 1 of 1

exception throwing and ?

Posted: Wed Sep 15, 2010 8:28 am
by Weiry
Hey guys,

This is just a quick one regarding exception handling when using the ? operator.
I have overloaded the default exception class and created a custom one, however i want to throw an exception using the following.
Code:

Code: Select all

try{
    $uObj = NULL;
    empty($uObj) ? $this->uObj = $uObj : throw new EAPIException('No User Object Defined',501);
}catch(EAPIException($e){
   print $e;
}
**Note: EAPIException has an overloaded __toString() function which is why im just printing $e.

For some reason this doesnt seem to work, nor does it generate an error in my error.log

There is very little info on the usage of ? :, so i thought id ask.

cheers.

Re: exception throwing and ?

Posted: Wed Sep 15, 2010 11:57 am
by AbraCadaver
Maybe a version difference, but I get:

[text]Parse error: syntax error, unexpected T_THROW[/text]

An IF would make much more sense here as you are "assigning" OR "throwing" not "assigning" A OR "assigning" B / "executing" A OR "executing" B.

Also, your expression states, IF $uObj IS empty ? assign it IF NOT empty : throw the exception. Maybe reverse the results or use !empty().

Re: exception throwing and ?

Posted: Wed Sep 15, 2010 5:02 pm
by Weiry
AbraCadaver wrote:Maybe a version difference, but I get:

[text]Parse error: syntax error, unexpected T_THROW[/text]

An IF would make much more sense here as you are "assigning" OR "throwing" not "assigning" A OR "assigning" B / "executing" A OR "executing" B.
I realise an IF would be effective in this case, im just trying to understand why something with a similar effect doesn't work.
What im basically trying to understand, why is it that when i use a basic true/false (IF or ?) doesn't the ? like to run a throw. Although its possible to run either a function or set a variable?
eg. empty($test) ? myfunction() : $e = NULL;
This doesn't generate any errors for me.
AbraCadaver wrote:Also, your expression states, IF $uObj IS empty ? assign it IF NOT empty : throw the exception. Maybe reverse the results or use !empty().
The result is the same regardless of which way i put it, still doesn't like it :S

Re: exception throwing and ?

Posted: Wed Sep 15, 2010 5:39 pm
by Eran
A ternary operator has a return value, unlike an if statement. You can't throw an exception in a statement that expects a value (like you can't do "return throw .. " in a function)

Re: exception throwing and ?

Posted: Wed Sep 15, 2010 6:42 pm
by AbraCadaver
pytrin wrote:A ternary operator has a return value, unlike an if statement. You can't throw an exception in a statement that expects a value (like you can't do "return throw .. " in a function)
Yes, that was my point with you are "assigning" OR "throwing" not "assigning" A OR "assigning" B / "executing" A OR "executing" B.. But pytrin obviously stated it succinctly.

Re: exception throwing and ?

Posted: Thu Sep 16, 2010 7:23 am
by Weiry
pytrin wrote:A ternary operator has a return value, unlike an if statement. You can't throw an exception in a statement that expects a value (like you can't do "return throw .. " in a function)
That makes a lot of sense now. I didn't quite understand the limitations of what the operator could do :)
cheers!

Re: exception throwing and ?

Posted: Thu Sep 16, 2010 9:19 pm
by John Cartwright
Weiry wrote:
pytrin wrote:A ternary operator has a return value, unlike an if statement. You can't throw an exception in a statement that expects a value (like you can't do "return throw .. " in a function)
That makes a lot of sense now. I didn't quite understand the limitations of what the operator could do :)
cheers!
You wouldn't have thought so based on your signature ;)