1

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 alright, 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
}
Johan Danforth
  • 4,469
  • 6
  • 37
  • 36

2 Answers2

1

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

  • InvalidOpertationException
  • ProtocolViolationException
  • NotSupportedException
  • WebException
Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
0

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

Community
  • 1
  • 1
Anton Gogolev
  • 113,561
  • 39
  • 200
  • 288