I have the following code snippet that does an HTTP request , but sometimes the URL does not work, then an exception will be thrown by the 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 staff adds a block try/catch
to control the flow of the program when giving this error, eg:
try
{
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
}
catch (WebException ex)
{
response = ex.Response as HttpWebResponse;
}
Is there another way of not stopping the flow of the program but without using try/catch
? I only want to use try
if it is the only solution.