开发者

Which exceptions should I watch for in a WebRequest call?

开发者 https://www.devze.com 2022-12-19 17:36 出处:网络
I\'m working on a small but vital batch application which has a step in it to download an image from a remote website I don\'t have any control over. This is quite simple and I got it working already

I'm working on a small but vital batch application which has a step in it to download an image from a remote website I don't have any control over. This is quite simple and I got it working already but I'd like some input on the error-handling.

The remote server doesn't always have the image requested so a 404 Not Found is actually alrig开发者_如何学JAVAht, but it's vital to catch all other communications errors, like timeouts and connection errors so that it can be put on a retry queue. I'd love to get suggestions about the exception-handring, anything in particular I should think about? Any other exception types that I should filter on?

try 
{
    // webrequest.getresponse(), read file and return image
}
catch (WebException webEx) 
{
    // check the WebException/http status code and act on certain errors
    // if 404 image/file not found - ok, just return
    // other error - log and put on retry queue
}
catch (Exception ex) 
{
    // some other major failure, log and alert
}


Please see HttpWebRequest.GetResponse. The documentation lists these potential exceptions:

  • InvalidOpertationException
  • ProtocolViolationException
  • NotSupportedException
  • WebException


See this. HttpWebRequest is ridden with so-called "vexing exceptions", so you'll have to watch out for them.

0

精彩评论

暂无评论...
验证码 换一张
取 消