Page 1 of 1
... or throw Exception
Posted: Sun Jan 16, 2005 2:24 pm
by andre_c
Does anyone know why this code throws a parse error?
Code: Select all
<?
$query = mysql_query( $query ) or throw new Exception( mysql_error() );
?>
Posted: Sun Jan 16, 2005 3:05 pm
by d3ad1ysp0rk
Could you.. possibly post the error?
Also, are you by chance running php4?
Re: ... or throw Exception
Posted: Sun Jan 16, 2005 3:09 pm
by infolock
andre_c wrote:Does anyone know why this code throws a parse error?
Code: Select all
<?
$query = mysql_query( $query ) or throw new Exception( mysql_error() );
?>
#1, $query is set to nothing...
#2, why not do it like this
Code: Select all
$result=mysql_query($query) or die(MySQL_Error());
Posted: Sun Jan 16, 2005 5:02 pm
by andre_c
sorry, i should have been more specific:
It's php 5 and the $query variable is a string with a valid sql statement (irrelevant, since it's giving me a parse error, not a fatal error or warning)
Code: Select all
Parse error: parse error, unexpected T_THROW in /www/youcaneauction.com/classes/Site.class.php on line 383
and i don't want to use ' or die( mysql_error() )' because it will only give me the mysql error, while throwing the exception will give me a stack trace.
Also i much rather catch the exception higher in the program hierarchy than kill the script because of a bad sql query
[edited] I am guessing that since a throw statement doesn't evaluate to true or false like functions do, it cannot be placed after a logical operator[/edited]
Posted: Sun Jan 16, 2005 7:09 pm
by timvw
so you could rewrite the code as
Code: Select all
$result = mysql_query($query);
if (!$result)
{
throw new Exception(mysql_error());
}
Posted: Mon Jan 17, 2005 1:32 am
by andre_c
thanks, that's exactly what i ended up doing