views:

24

answers:

2

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
}
A: 

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

Anton Gogolev
+1  A: 

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

  • InvalidOpertationException
  • ProtocolViolationException
  • NotSupportedException
  • WebException
Andrew Hare