PHP:尝试并捕获异常

Here is my try catch in PHP

try{
    print (string)((int)$screenname+ 1);
    $query2 = 'SELECT * FROM callerdetail WHERE screenname="'.(string)((int)$screenname+ 1).'" AND status="0" AND agent = "'.$agent.'" LIMIT 1;';
    $result =   mysql_query($query2) or die (mysql_error());

}
catch(Exception $e){
    $query3 = 'SELECT * FROM callerdetail WHERE status="0" AND agent = "'.$agent.'" LIMIT 1';
    $result =   mysql_query($query3) or die (mysql_error());
}

I am trying to get the data based on screenname and if it is not available then i would like to execute the catch block sql statement .

Unfortunately it is not working for me . I have a data in my database having screen name =2 but instead of getting that data I am getting the new data for agent

Any Idea ?

From the documentation:

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

So, your catch block is never called because mysql_query does not throw a exception.

Use a if construct, or throw the exception yourself.

you have ; in query in try block

$query2 = 'SELECT * FROM callerdetail WHERE screenname="'.(string)((int)$screenname+ 1).'" AND status="0" AND agent = "'.$agent.'" LIMIT 0,1';

and use limit properly

Why would an 'exception be thrown?' Nothing is causing an 'exception'.

Would an if statement not be better?

$query2 = 'SELECT * FROM callerdetail WHERE screenname="'.(string)((int)$screenname+ 1).'" AND status="0" AND agent = "'.$agent.'" LIMIT 1;';
$result =   mysql_query($query2) or die (mysql_error());

if(mysql_num_rows($result) > 0 ){ // Got a result
    // Do something
) else {
    // Execute other query
    $query3 = 'SELECT * FROM callerdetail WHERE status="0" AND agent = "'.$agent.'" LIMIT 1';
    $result =   mysql_query($query3) or die (mysql_error());
}