0

I've got code that works when I pass a recognized argument, but when the arg I pass is not recognized, I get an error message. I don't necessarily expect (or need) a good response in every case, but I don't want to "bothered" by an exception when there are no results to return to me.

I'll give a couple of examples. If I pass this to (HttpWebRequest)WebRequest (using "tt0003854" as the arg):

https://api.themoviedb.org/3/movie/tt0003854/release_dates?api_key=Gr8GooglyMoogly&language=en-US&external_source=imdb_id

...I get what I want back:

{"id":347745,"results":[{"iso_3166_1":"US","release_dates":[{"certification":"","iso_639_1":"","note":"","release_date":"1936-12-12T00:00:00.000Z","type":3}]}]}

And the same is true for other attempts. Some, though, fail, such as when I use "tt0005929" as the arg:

https://api.themoviedb.org/3/movie/tt0005929/release_dates?api_key=Gr8GooglyMoogly&language=en-US&external_source=imdb_id

...which returns:

{"success":false,"status_code":34,"status_message":"The resource you requested could not be found."}

It fails on this line:

var webResponse = (HttpWebResponse)webRequest.GetResponse();

...and falls to the catch block, at which point I get an err msg that says, "The remote server returned an error: (404) Not Found"

It's "okay" if it's not found, but I don't want the app to be stopped by an error message. What can I do to ignore the "404"s?

Here is more of my code, for context:

try
{
    var webRequest = (HttpWebRequest)WebRequest.Create(RESTStringToGetMPAARatingForMovieId);
    webRequest.Method = "GET";  // <-- GET is the default method/verb, but it's here for clarity
    var webResponse = (HttpWebResponse)webRequest.GetResponse();
    if (webResponse.StatusCode == HttpStatusCode.NotFound)
    {
        continue; // this is not reached, even when I get the error
    }
    if ((webResponse.StatusCode != HttpStatusCode.OK) || (webResponse.ContentLength == 0))
    {
        continue; // this is not reached, either
    }
    if ((webResponse.StatusCode == HttpStatusCode.OK) && (webResponse.ContentLength > 0))
    {
        StreamReader streamReader = new StreamReader(webResponse.GetResponseStream());
        string s = streamReader.ReadToEnd();
        . . .    
    }
    else
    {   // I don't see this message
        MessageBox.Show(string.Format("Status code == {0}, Content length == {1}",
          webResponse.StatusCode, webResponse.ContentLength));
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • 1
    _"I don't want the app to be stopped"_ ...as you're already catching the exception then you aren't stopping the app - it won't crash, and can continue executing. It's unclear what the problem is. Can you clarify what your specific concern is in this scenario? – ADyson Sep 23 '20 at 22:23
  • 1
    P.S. If you still want to be able to read the actual response body (as opposed to just the status / error message), then [this](https://stackoverflow.com/a/10083100/5947043) might be of interest. – ADyson Sep 23 '20 at 22:30

1 Answers1

1

First: Is the cast or the method call throwing the error? (Simply split the call and cast to two lines) Second: You can put a try {...} catch(Exception ex) /*{MessageBox.Show(Ex.Message)*/} block around the line, however, you must make sure that the later code works even with the wrong value, make sure to test it.

Nekuskus
  • 143
  • 1
  • 11