0

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

public static void mymethod()
{
    HttpWebRequest 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();
}
3
  • @Jeff Mercado No its the damn browser Commented Aug 26, 2011 at 9:04
  • 1
    @avatishchev Error 4 Only assignment, call, increment, decrement, and new object expressions can be used as a statement Commented Aug 26, 2011 at 9:05
  • @James: please ask explicit questions. Yours turns out not to be about exception handling but about a simple syntax error. Always list the error message. Commented Aug 26, 2011 at 9:18

6 Answers 6

2

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);
    }      
}
Sign up to request clarification or add additional context in comments.

Comments

1

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()

2 Comments

sorry I don't see what the purpose of this is, can you exaplain
@James: First of all, using block can't be used in one line, it should have underlying scope. Or just don't use it.
0

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); 

Comments

0

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.

Comments

0

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.

Comments

-1

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())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.