5
I have the following code snippet that makes an HTTP request, only that sometimes the URL does not work, then will be launched an exception by framework.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = WebRequestMethods.Http.Head;
// vai ser lançada uma exceção nessa linha
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
I found examples on the internet that people add a block try/catch
to control the program flow when this error occurs, example:
try
{
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
}
catch (WebException ex)
{
response = ex.Response as HttpWebResponse;
}
There is another way not to stop the program flow but without using try/catch
? I just want to use try
if it is the only solution.
Thanks for remembering, @Augustovasques. Even not having many 'upvotes' in my reply, I made the correction and remark that you mentioned, not to encourage the wrong use.
– Minelli