PHP如果函数有错误

时间:2013-12-27 18:31:06

标签: php function if-statement error-handling

我的PHP代码中有一行看起来像这样(它调用一个函数):

$objTest->joinServer($servers[array_rand($servers)]);

但是,有时,函数/行会抛出在无限循环中继续的错误。如何检测此行是否会返回错误,反之亦然?

目前,我认为我可能需要做这样的事情,但我不知道如何正确实现它:

$conn = $objTest->joinServer($servers[array_rand($servers)]);

if($conn has some error) {
    // Do this
} else {
    // No error so excute line
    $objTest->joinServer($servers[array_rand($servers)]);
}

1 个答案:

答案 0 :(得分:1)

try{
    $conn = $objTest->joinServer($servers[array_rand($servers)]);
catch(Exception e) {
    //do something else here
}

如果调用该函数时发生错误,您可以输出错误:     如你所说,e->getMessage()或'做别的事'。阅读文档:http://www.php.net/manual/en/language.exceptions.php