Why is the code in the try block separate from the rest of the methods?

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?

+3
source share
8 answers

.

request . try , # , , .

, WebRequest.Create , request .

, catch, :

HttpWebRequest request = null;

, catch.
Uri.TryCreate.

+12

, , HttpWebRequest request = null;.

# C- ( JavaScript!) -, .

request.

//create new scope (every '{ }' block has it own scope, so you can also create
// a new one, by just wrapping some code inside accolades.
{
    if(a) request = something;
    else if(b) // do nothing
}

request.DoSomething();

, , , a, request. try-catch. try catch .


, :

// first-part-of-my-app
{
    int myVariable = 10;
}

// second-part
{
    string myVariable = "hi"; // is valid
}
+6

, , .

HttpWebRequest request;

try {
    request = (HttpWebRequest) WebRequest.Create(url);
    HttpWebResponse response  = (HttpWebResponse) request.GetResponse();
    // do stuff with your response
} catch (UriFormatException) {
    statusLabel.Text = "The address you entered was malformed, please correct it.";
    statusLabel.ForeColor = Color.Red;
}
+2

:

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;
    return;
}

HttpWebResponse response  = (HttpWebResponse) request.GetResponse();

,

+2

HttpWebRequest request = null;

try .

, , HttpWebRequest.

0

null , , , .

0

, :

, . , .

, , , .

#, . , , , . , .

try ( ) , . , , , ( ).

, , , .NET Framework , . , HttpWebRequest request; , . , , , , " ", , ( ) , , request .

, :

HttpWebRequest request = null;

try 
{ 
    request = (HttpWebRequest) WebRequest.Create(url); 
}
catch (UriFormatException)
{ 
    statusLabel.Text = "The address you entered was malformed, please correct it."; 
    statusLabel.ForeColor = Color.Red; 
} 

if (request != null)
{  
   HttpWebResponse response = (HttpWebResponse) request.GetResponse(); 
}
0
source

This is due to the fact that, unfortunately, you can wrap the first line to

HttpWebRequest request = null;

and this will fix it, basically, the code has scope with the try block, so nothing outside of it will be known that the value is set

-2
source

Source: https://habr.com/ru/post/1731223/


All Articles