1

I have a HTTP class that gets content from URL's, POST's content to URL's etc and then returns the raw HTML content.

In the function inside of the class it detects if there is a HTTP error and if so I would like to return false but will this work if I have declared the function to return a String?

Code Sample of what I am trying to do (Note the Return Content & Return False if a HTTP error code is detected)

Public Function Get_URL(ByVal URL As String) As String
    Dim Content As String = Nothing
    Try
        Dim request As Net.HttpWebRequest = Net.WebRequest.Create(URL)
        ' Request Settings
        request.Method = "GET"
        request.KeepAlive = True
        request.AllowAutoRedirect = True
        request.Timeout = MaxTimeout
        request.CookieContainer = cookies
        request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.60 Safari/534.24"
        request.Timeout = 60000
        request.AllowAutoRedirect = True
        Dim response As Net.HttpWebResponse = request.GetResponse()
        If response.StatusCode = Net.HttpStatusCode.OK Then
            Dim responseStream As IO.StreamReader = New IO.StreamReader(response.GetResponseStream())
            Content = responseStream.ReadToEnd()
        End If
        response.Close()
    Catch e As Exception
        HTTPError = e.Message
        Return False
    End Try
    Return Content
End Function

And usage example:

Dim Content As String = Get_URL("http://www.google.com/")
If Content = False Then
    MessageBox.Show("A HTTP Error Occured: " & MyBase.HTTPError)
    Exit Sub
End If

1 Answer 1

1

Usually in this type of scenario, you would throw a new exception with more detailed information, and let the exception bubble up to the processed by the main code (or just let the original exception bubble up without Catching it in the first place).

Catch e As Exception
    ' wrap the exception with more info as a nested exception
    Throw New Exception("Error occurred while reading '" + URL + "': " + e.Message, e)
End Try

Inside the usage example:

Dim content As String = ""
Try
    content = Get_URL("http://www.google.com/")
Catch e As Exception
    MessageBox.Show(e.Message)
    Exit Sub
End Try
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks a lot for the help, this is exactly what I was trying to do. Still really new to VB but learning more each day! Will Throw New Exception within the function break out of the function or will it still run the Return Content underneath of the End Try?
@Chris: It will break out of the function immediately, and thus content will still be set to it's original value of "".
Update: Throwing a new exception also displays a messagebox, I deal with all errors internally and log them to a database so have no need for the messagebox display. Is there a way to throw the exception back up the line without the prompt showing?
You can remove the line MessageBox.Show to silently exit the method while still catching/logging the message and not continuing.
lol, stupid move by myself there :-) Thanks again for the help :-)

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.