Error handling for time out with Http Client


I’m trying to use the Http Client to return status codes for urls, however when the request timesout the script fails entirely

$http = new Client();
$response = $http->get($links[$i]['url'],[],['timeout' => '10']);
$links[$i]['http_status'] = $response->statusCode();

I’d like to acknowledge the error, but keep looping through my list of urls. Simply extending the timeout is not enough as I have some urls that are no longer valid domains.


The solution was to use try/catch. I had tried this initially but missed the \ before Exception on the catch

try {
    // code
} catch (\Exception $e) {
    // error