8

Is there a "static" way of throwing an exception in php?

I need to throw an exception when a mysql query fails.

I tried this:

$re=@mysql_query( $query ) or throw new Exception(' Query Failed ');

but it's not working.

And I'm using a function based on the throwException() function from this comment at PHP: Exceptions manual, but I would like to know if there is a static way for doing this without making a class.

Kamafeather
  • 8,663
  • 14
  • 69
  • 99
sreejith
  • 779
  • 3
  • 11
  • 20

3 Answers3

7

The comment you link to states that throwing an exception in this way will not work. It states you have to place it in a function:

function throwException($message = null, $code = null) {
    throw new Exception($message, $code);
}

$re = mysql_query($query) or throwException('Query Failed');
webbiedave
  • 48,414
  • 8
  • 88
  • 101
  • yeah it does. I was looking for a way to do something like Exception::setMessage(' Message '); But now I know it's not possible after BoltClock replied – sreejith Feb 05 '11 at 08:32
  • @rados: And he's saying you call the function after `or`, but in your question you're calling the actual `throw` statement instead of the function. – BoltClock Feb 05 '11 at 08:33
  • @BoltClock yes, what I'm doing now is something like this: $re=@mysql_query( $query) or IException::throwException(' Query Failed '); IException is a class I made myself, I was looking for a pre-defined class instead of using my own one. – sreejith Feb 05 '11 at 08:37
4

You won't be able to directly do or throw new Exception(); because throw is a statement, not an expression. Since or is really an operator, it expects its operands to be expressions (things that evaluate to some values).

You'd have to do this instead:

$re = mysql_query($query);

if (!$re) {
    throw new Exception('Query Failed');
}

If you're trying to use the throwException() function proposed by that PHP manual comment, as webbiedave points out the comment is saying that you need to call that function instead of the throw statement directly, like this:

$re = mysql_query($query) or throwException('Query Failed');

There's no rule in PHP that says you need to throw exceptions from a class method. As long as there's some way to catch that exception you're fine. If you mean you want to throw exceptions without using the Exception class, well, you have to. Exceptions are objects by nature; you can't throw an exception that isn't an object (or doesn't inherit from the Exception class).

If you don't want to throw exceptions but raise the kind of error you often see from PHP (notices, warnings and fatal errors), use trigger_error().

$re = mysql_query($query);

if (!$re) {
    trigger_error('Query Failed', E_USER_ERROR);
}
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • Everything in imperative programming languages is a statement ;) `throw` is a language construct, so it's dealt with by parser and can't be evaluated runtime to some values, yes – meze Feb 05 '11 at 08:55
2

You might also try something like

mysql_query($query);

if (mysql_error()) {
  throw new Exception(...);
}

as mysql_error() returns empty string if there was no error, which evaluates to false.

Jakub Arnold
  • 85,596
  • 89
  • 230
  • 327