Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a static way to throw exception in php

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.

like image 587
sreejith Avatar asked Feb 05 '11 08:02

sreejith


1 Answers

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');
like image 149
webbiedave Avatar answered Sep 20 '22 02:09

webbiedave