try()-catch()中的问题 [英] Problem in try()-catch()

查看:89
本文介绍了try()-catch()中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


HttpRes在尝试"部分中进行了初始化,但我无法在尝试"部分中的HttpRes中使用相同的数量


HttpRes take initialize in section Try, but i can''t use HttpRes in section Catch with the same amount in Try

   try
        {
            //Code Example
            string url = "http://www." + TextBox1.Text;
            HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(url);
            httpReq.AllowAutoRedirect = false;

            HttpWebResponse httpRes = (HttpWebResponse)httpReq.GetResponse();
          
                Label1.Text = (((HttpWebResponse)httpRes).StatusDescription);
           
            // Close the response.
            httpRes.Close();
        }

catch
        {

            Label1.Text = (((HttpWebResponse)httpRes).StatusDescription);
               
        }

推荐答案



仔细查看您的代码.您没有关闭try块.

Hi,

Look into you code carefully. You are not closing your try block.

try
        {
            //Code Example
            string url = "http://www." + TextBox1.Text;
            HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(url);
            httpReq.AllowAutoRedirect = false;
 
            HttpWebResponse httpRes = (HttpWebResponse)httpReq.GetResponse();
          
                Label1.Text = (((HttpWebResponse)httpRes).StatusDescription);
           
            // Close the response.
            httpRes.Close();
         }//This was not there you missed.
catch
        {
 
            Label1.Text = (((HttpWebResponse)httpRes).StatusDescription);
               
        }


只需在尝试使用Block之前声明httpRes
Just Declare The httpRes Before Try Block
 HttpWebResponse httpRes;
try
        {
            //Code Example
            string url = "http://www." + TextBox1.Text;
            HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(url);
            httpReq.AllowAutoRedirect = false;
 
            httpRes = (HttpWebResponse)httpReq.GetResponse();
          
                Label1.Text = (((HttpWebResponse)httpRes).StatusDescription);
           
            // Close the response.
            httpRes.Close();
        }
 
catch
        {
 
            Label1.Text = (((HttpWebResponse)httpRes).StatusDescription);
               
        }


u像这样尝试之前先声明您的httpRes变量

u declare your httpRes variable before the try like this

HttpWebResponse httpRes;
try
        {
            //Code Example
            string url = "http://www." + TextBox1.Text;
            HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(url);
            httpReq.AllowAutoRedirect = false;
 
            httpRes = (HttpWebResponse)httpReq.GetResponse();
          
                Label1.Text = (((HttpWebResponse)httpRes).StatusDescription);
           
            // Close the response.
            httpRes.Close();
         }//This was not there you missed.
catch
        {
 
            Label1.Text = (((HttpWebResponse)httpRes).StatusDescription);
               
        } 


这篇关于try()-catch()中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆