简体   繁体   中英

httprequest exception handling php

In case a request cannot be made, how can I catch the exception?

I have so far written this, but not had any luck.

$url = 'http://localhost:49000/';
//create the httprequest object                
try{
  $httpRequest_OBJ = new httpRequest($url, HTTP_METH_POST, $options);
}catch(HttpException $e){
  echo $e;
}

I had the same problem, you should make sure that the exception is in the namespace.

What I've done is:

try {
    //Request here
} catch (\HttpException $e) {
    //Handle exception
} catch (\Exception $e) {
    //Just in case
}

Also good to notice what is pointed in this article http://www.mkfoster.com/2009/01/06/how-to-pecl-http-request-exception-and-error-handling/ about the inner exception, in case you want the message returned by the exception.

Also, there is a php.ini directive that forces HttpRequest to always throw exception http://br.php.net/manual/en/http.configuration.php http.only_exceptions=1

how can I catch the exception?

To catch an exception you need to know the classname of the exception. What is the classname of the exception you'd like to catch?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM