开发者

Compiler error in using block

开发者 https://www.devze.com 2023-03-31 12:06 出处:网络
I am trying to catch any errors that crop up when a URL is invalid, below is the original code: public static void mymethod()

I am trying to catch any errors that crop up when a URL is invalid, below is the original code:

public static void mymethod()
{
    Ht开发者_JAVA百科tpWebRequest request = (HttpWebRequest)WebRequest.Create(firstline);
    Stopwatch timer = new Stopwatch();
    timer.Start();                
    using (var response = request.GetResponse())    
    timer.Stop();
    timeTaken = timer.Elapsed.ToString();
}

I have tried to create an exception handler as below, but no luck:

public static void mymethod()
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(firstline);
    Stopwatch timer = new Stopwatch();
    timer.Start();

    try
    {            
        using (var response = request.GetResponse())
    }    
    catch
    {
        Console.WriteLine("error here...")
    }

    timer.Stop();
    timeTaken = timer.Elapsed.ToString();
}


You should do something like this.

public static void ShowResponseAndTimeTaken(string firstline)
{
    try
    {   
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(firstline);
        System.Diagnostics.Stopwatch timer = new Stopwatch();
        timer.Start();
        using (var response = request.GetResponse())
        {
            Console.WriteLine("Response : {0}", response);
        }
        timer.Stop();
        Console.WriteLine("Time taken : {0}", timer.Elapsed); 
    }
    catch(Exception e)
    {
        Console.WriteLine("error : {0}", e.Message);
    }      
}


Your usage of using-block is incorrect. The correct looks like this:

using (var response = request.GetResponse())
{
}

or just without the block:

var response = request.GetResponse();
response.Dispose()


I believe you cannot intercept it because it is thrown before "try" block: it is possible that "firstline" variable contains a text with perhaps incorrectly formatted uri,

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(firstline); 


You can also validate URL using regex instead of waiting for exception to be thrown - http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.aspx.

Here (http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.getresponse.aspx) you can find detailed description about GetResponse. Check what exceptions can be thrown and modify your current exception handling. You shouldn't leave try...catch as it is. At least catch Exception class.


WebRequest.Create only throws if the scheme is invalid (the bit before the colon), there is a security error or if the URI you pass is null (although you have not enclosed that statement inside your try statement so such errors will be unhandled by the above code anyway).

To handle other errors, you should look at the HttpWebResponse.StatusCode property and handle the HTTP errors in way appropriate to your application.


I think you are talking about syntactic errors, because your code does not compile?

Try to add the missing semicolon on this line:

Console.WriteLine("error here...")

And the using-block contains no block:

using (var response = request.GetResponse())

remove the using:

var response = request.GetResponse())
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号