0

I have a line in my PHP code that looks like this (it calls a function):

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

However, sometimes, the function/line throws back an error that continues in an infinite loop. How can I detect if this line throws back an error, and so something else instead?

Currently, I think that I may need to do something like this, but I have no idea how to implement it correctly:

$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 Answer 1

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

If an error occurs calling that function, you can output the error with: e->getMessage() or 'do something else' as you said. Read the docs: http://www.php.net/manual/en/language.exceptions.php

Sign up to request clarification or add additional context in comments.

2 Comments

I'm still getting the error. However, the error I'm getting is a Notice message, does that matter?
@user2898075 that's the point, since you are catching the error... what is the message?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.