PHP: 'or' statement on instruction fail: how to throw a new exception?


PHP: 'or' statement on instruction fail: how to throw a new exception?



Everyone here should know the 'or' statemens, usually glued to an die() command:

$foo = bar() or die('Error: bar function return false.'); 

The most of the times we see something like:

mysql_query('SELECT ...') or die('Error in during the query'); 

However, i cant understand how exactly that 'or' statement works.

I would like to throw a new exception instead of die(), but:

try{     $foo = bar() or throw new Exception('We have a problem here'); 

Doesnt work, and neither

$foo = bar() or function(){ throw new Exception('We have a problem here'); } 

The only way i found to do that is this horrible thought:

function ThrowMe($mess, $code){     throw new Exception($mess, $code); } try{     $foo = bar() or ThrowMe('We have a problem in here', 666); }catch(Exception $e){     echo $e->getMessage(); } 

But there is a way to throw a new exception directly after the 'or' statement?

Or this kind of structure is mandatory (i dont liek the ThrowMe function at all):

try{     $foo = bar();     if(!$foo){         throw new Exception('We have a problem in here');     } }catch(Exception $e){     echo $e->getMessage(); } 

Edit: what i want is really to avoid the use of an if() check every potential dangerous operation i do, for example:

#The echo $e->getMessage(); is just an example, in real life this have no sense! try{     $foo = bar();     if(!$foo){         throw new Exception('Problems with bar()');     }     $aa = bb($foo);     if(!$aa){         throw new Exception('Problems with bb()');     }     //...and so on! }catch(Exception $e){     echo $e->getMessage(); }  #But i relly prefer to use something like:  try{     $foo = bar() or throw new Exception('Problems with bar()');     $aa = bb($foo) or throw new Exception('Problems with bb()');     //...and so on! }catch(Exception $e){     echo $e->getMessage(); }  #Actually, the only way i figured out is:  try{     $foo = bar() or throw new ThrowMe('Problems with bar()', 1);     $aa = bb($foo) or throw new ThrowMe('Problems with bb()', 2);     //...and so on! }catch(Exception $e){     echo $e->getMessage(); }  #But i'll love to thro the exception directly instead of trick it with ThrowMe function. 



Keep getting exceptions using PrincipalContext from the System.DirectoryServices.AccountManagement assembly

1:



C# Exception handling in classes
or is just a logical operator, and it's analogous to ||..
Delphi: EReadError with message 'Property Persistence does Not Exist'
The common trick of.
Python exceptions: call same function for any Exception
mysql_query() or die(); 
could just as well be written.
Handing exception in BLL and return to client (either winforms or webforms)?
mysql_query() || die(); 
What happens here is the "logical or" operator (whichever you choose) is trying to determine if either operand evaluates to TRUE.


Python - Possible to force raise exceptions regardless try/except blocks?
This means the operands must be expressions that can be cast as a boolean..
What does an “Unable to validate data” exception in System.Web.Configuration.MachineKeySection.GetDecodedData indicate
So, the reason.
In delphi 7, is `try … except raise; end;` meaningful at all?
bar() or throw new Exception(); 
is illegal, is because.
(boolean)throw new Exception(); 
is also illegal.

In essence, the process of throwing an exception doesn't generate a return value for the operator to check.. But calling a function does generate a return value for the operator to check (no explicit return value will result int the function returning NULL which casts as FALSE) which is why it works for you when you wrap exception throwing in a function.. Hope that helps..


2:


Why don't bar() and bb() throw the exceptions? In PHP Exceptions bubble up, so there's no need to throw the exception in the function/method where you call bar()/bb().

These exceptions may be thrown by bar()/bb().

In case you want to throw another exception, you can simply do:.
function foo() {     try {         $bar = bar();     } catch (BarException) {         throw new FooException;     } } 


3:


I think you want to use something like the last structure, although there's really no point in using exceptions for that:.
$foo = bar(); if(!$foo){     echo 'We have a problem in here'; } 
Per comment - I don't think you can do that in a single line (i.e.

without the if(!$foo) check), and I agree that the exception throwing method is pretty horrible.

Personally, I prefer the explicitness of:.
$foo = bar(); if(!$foo){     throw new Exception('We have a problem in here'); } 
but that's a personal preference.

If you want the single-line thing, I think you'll have to go with your exception-throwing function option.. I guess this limitation is probably down to PHP's dynamic-typing stuff, it can cast the results of a function call to a conditional, but not the results of a throw..


4:


Here's a single-line solution without the extra function:.
if (!($foo = bar())) throw new Exception('We have a problem here'); 



99 out of 100 based on 59 user ratings 759 reviews