My problem looks something like this:
HttpWebRequest request;
try {
request = (HttpWebRequest) WebRequest.Create(url);
} catch (UriFormatException) {
statusLabel.Text = "The address you entered was malformed, please correct it.";
statusLabel.ForeColor = Color.Red;
}
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
The error received from this is that requestno value has been assigned. Obviously, this is because the value for the request is set in the try block.
The reason why this bothers me is that in other languages that I used, the code in the block is trynot stored separately (did I forget the word for this, maybe encapsulation?) From the rest of the code - similar to the method.
Am I really wrong? Should I duplicate the code in the try block after the exception, assuming that it WebRequestdoesn't throw one?
source
share