title: how to handle WebException in ASP.Net Web Application using c#?
body: <p>I'm creating my own weather api by calling third party api openweather. I don't know how to handle WebException which occurs when I provide city which doesn't exist as an input, it gives me status code 404 from openweatherapi and I don't know how to handle that error my application always crashes due to unhandled exception</p>
  <p>Thank you for helping :D</p>
  <p>code on which I'm working</p>
  <pre><code>        public void WeatherDetail(string City)
          {
               
          //Assign API KEY which received from OPENWEATHERMAP.ORG  
              string appId = &quot;*******&quot;;
  
              //API path with CITY parameter and other parameters.  
              string url = string.Format(&quot;http://api.openweathermap.org/data/2.5/weather?q={0}&amp;appid={1}&quot;, City, appId);
  
              using (WebClient client = new WebClient())
              {
                  string json = &quot;&quot;;
                  try
                  {
                      json = client.DownloadString(url);
                  }
                  catch (system.net.webexception)
                  {
                      // how to handle WebException ?
                  }
                  
  
                  //Converting to OBJECT from JSON string.  
                  Root weatherInfo = (new JavaScriptSerializer()).Deserialize&lt;Root&gt;(json);
  
                  //Special VIEWMODEL design to send only required fields not all fields which received from   
                  ResultViewModel rslt = new ResultViewModel();
                  DateTime aDate = DateTime.Now;
                  string today = aDate.ToString(&quot;yyyy-MM-dd&quot;);
  
                  rslt.Country = weatherInfo.sys.country;
                  if(City == &quot;Thane&quot; || City == &quot;thane&quot;)
                  {
                      rslt.Name = &quot;Thane&quot;;
                  }
                  else
                  {
                      rslt.Name = weatherInfo.name;
                  }
                  rslt.Lat = weatherInfo.coord.lat;
                  rslt.Lon = weatherInfo.coord.lon;
                  rslt.Description = weatherInfo.weather[0].description;
                  rslt.Temp = weatherInfo.main.temp;
                  rslt.WeatherIcon = weatherInfo.weather[0].icon;
                  rslt.Pressure = weatherInfo.main.pressure;
                  rslt.Deg = weatherInfo.wind.deg;
                  rslt.WindSpeed = weatherInfo.wind.speed;
                  rslt.Main = weatherInfo.weather[0].main;
                  rslt.WeatherId = weatherInfo.weather[0].id;
                  rslt.Sunrise = weatherInfo.sys.sunrise;
                  rslt.Sunset = weatherInfo.sys.sunset;
                  rslt.Date = today;
                  rslt.Id = weatherInfo.id;
                  try
                  {
                      con.Open();
  
                  }
                  catch (System.Data.SqlClient.SqlException ex)
                  {
                      throw ex;
                  }
                      cmd = new SqlCommand(&quot;insert into CityWeather (lon, lat, name, country, weatherid, main, description, temp, pressure, sunrise, sunset, windspeed, deg, weatherIcon) values (&quot; +
                          &quot;'&quot; + rslt.Lon + &quot;','&quot; + rslt.Lat + &quot;','&quot; + rslt.Name + &quot;','&quot; + rslt.Country + &quot;', '&quot; + rslt.WeatherId + &quot;','&quot; + rslt.Main + &quot;', '&quot; + rslt.Description + &quot;','&quot; + rslt.Temp + &quot;', '&quot; + rslt.Pressure + &quot;', '&quot; + rslt.Sunrise + &quot;','&quot; + rslt.Sunset + &quot;', '&quot; + rslt.WindSpeed + &quot;', '&quot; + rslt.Deg + &quot;', '&quot; + rslt.WeatherIcon + &quot;')&quot;
                          , con);
                      cmd.ExecuteNonQuery();
                      con.Close();
              }
          } 
  </code></pre>
  <p>Thank you for helping :D</p>