try { // Code that might throw an exception } catch (Exception $e) { $response = $e->getResponse(); echo $response->getStatusCode(); // returns the HTTP status code echo $response->getBody(); // returns the response body }
try { // Code that might throw an exception } catch (Exception $e) { $response = $e->getResponse(); if ($response === null) { echo "There was an error, but no response was received."; } else { echo $response->getBody(); // returns the response body } }In this example, we are again using the try-catch statement to catch any exceptions thrown in the code block. However, we are checking if the response is null before trying to use it. If the response is null, we simply output an error message. If not, we extract and output the response body. Package library: This code example could be used in any PHP application as it does not rely on any specific package library.