Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 722595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:00:31+00:00 2026-05-14T06:00:31+00:00

I am trying to handle two different WebException ‘s properly. Basically they are handled

  • 0

I am trying to handle two different WebException‘s properly.

Basically they are handled after calling WebClient.DownloadFile(string address, string fileName)

AFAIK, so far there are two I have to handle, both WebException‘s:

  • The remote name could not be resolved (i.e. No network connectivity to access server to download file)
  • (404) File not nound (i.e. the file doesn’t exist on the server)

There may be more but this is what I’ve found most important so far.

So how should I handle this properly, as they are both WebException‘s but I want to handle each case above differently.

This is what I have so far:

try
{
    using (var client = new WebClient())
    {
        client.DownloadFile("...");
    }
}
catch(InvalidOperationException ioEx)
{
    if (ioEx is WebException)
    {
        if (ioEx.Message.Contains("404")
        {
            //handle 404
        }
        if (ioEx.Message.Contains("remote name could not")
        {
            //handle file doesn't exist
        }
    }
}

As you can see I am checking the message to see what type of WebException it is. I would assume there is a better or a more precise way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T06:00:31+00:00Added an answer on May 14, 2026 at 6:00 am

    Based on this MSDN article, you could do something along the following lines:

    try
    {
        // try to download file here
    }
    catch (WebException ex)
    {
        if (ex.Status == WebExceptionStatus.ProtocolError)
        {
            if (((HttpWebResponse)ex.Response).StatusCode == HttpStatusCode.NotFound)
            {
                // handle the 404 here
            }
        }
        else if (ex.Status == WebExceptionStatus.NameResolutionFailure)
        {
            // handle name resolution failure
        }
    }
    

    I’m not certain that WebExceptionStatus.NameResolutionFailure is the error you are seeing, but you can examine the exception that is thrown and determine what the WebExceptionStatus for that error is.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a good way to handle all possible collisions between two
Situation: I am trying to construct a simple method that accepts two different integers
I'm trying to verify if data exists in two different tables in a single
I'm trying handle bad json data when parsed through json_decode(). I'm using the following
I'm trying to handle a JSONP Callback, I have the following JavaScript var URL
I am trying to handle a case of a DateTime which might be null
I'm trying to handle a file address within my python program and pass it
I'm trying to handle the problem where a user can try to open, with
I am trying to handle a contenteditable body in an iframe, in order to
I've been trying to handle the onkeydown event across multiple frames (no, I unfortunately

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.