exception throwing and ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

exception throwing and ?

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: exception throwing and ?

Post 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().
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: exception throwing and ?

Post 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
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: exception throwing and ?

Post 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)
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: exception throwing and ?

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: exception throwing and ?

Post 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!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: exception throwing and ?

Post 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 ;)
Post Reply