6

I am getting Read timeout error while calling a third party REST API.

Can I catch it using try catch block? Or it is not catchable like other Limit Exceptions. Please explain.

4
  • did you try to catch it and couldn't?
    – Saariko
    Sep 27 '12 at 9:28
  • I tried but this error occurs time to time and i am calling third party REST API. So i don't have much control over it.
    – doga
    Sep 27 '12 at 9:35
  • 1
    can you paste some code?
    – Saariko
    Sep 27 '12 at 9:52
  • 6
    Try setting the timeout to 60 secs, req.setTimeout(60000); Sep 27 '12 at 14:00
13

Yes, you can catch the CalloutException; from the Apex Web Services and Callouts wiki article:

try {
    //Execute web service call here     
    HTTPResponse res = http.send(req);  

    //Helpful debug messages
    System.debug(res.toString());
    System.debug('STATUS:'+res.getStatus());
    System.debug('STATUS_CODE:'+res.getStatusCode());       
} catch(System.CalloutException e) {
    //Exception handling goes here.... retry the call, whatever
}

As logontokartik mentions in the comment above, you can also set the timeout on the callout:

req.setTimeout(60000); // timeout in milliseconds - this is one minute

The default timeout is 10 seconds. The minimum is 1 millisecond and the maximum is 120 seconds.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.