Results 1 to 3 of 3

Thread: [RESOLVED] Why the Warning with my 'try catch finally end try' code?

  1. #1

    Thread Starter
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Resolved [RESOLVED] Why the Warning with my 'try catch finally end try' code?

    Why do i get a warning with my http? Trying to come to terms with try catch finally end try
    Code:
            Dim myhttp As HttpWebRequest
            Dim html As String = ""
            Dim responseStream As Stream
            Dim WebResponse As HttpWebResponse
            Try
                myhttp = CType(WebRequest.Create(url), HttpWebRequest)
                WebResponse = CType(myhttp.GetResponse(), HttpWebResponse)
                responseStream = WebResponse.GetResponseStream()
                Dim reader As StreamReader = New StreamReader(responseStream, Encoding.Default)
                html = reader.ReadToEnd()
    
            Catch ex As HttpException
                MsgBox("Validation HTTP error" & vbCrLf & ex.Message)
            Catch ex As NullReferenceException
                MsgBox("Null value returned error")
            Catch ex As Exception
                MsgBox("Validation Other Exception error" & vbCrLf & ex.Message)
            Finally
                responseStream.Close()
            End Try
    the code in the finally block to close the stream that may or may not have produced an error gives me a warning that the code may produce a null value, even with both an exception handler and with a null value handler.

    I read about using nested try catch end try and wonder if i should try again if i get an error. Is it possible to say do the block if it errors give the user the option to try again or abort the app?

    (afterthought this is sending an encrypted string to a server to validate before the app gives full access)
    Last edited by Megalith; Jan 3rd, 2010 at 10:12 PM.
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  2. #2
    Hyperactive Member Philly0494's Avatar
    Join Date
    Apr 2008
    Posts
    485

    Re: Why the Warning with my 'try catch finally end try' code?

    in your 3rd line try putting this instead of "Dim responseStream As Stream"

    Code:
    Dim responseStream As Stream = Nothing
    you are getting an error because you are performing responseStream.Close() when responseStream may or may not have been initialized (if an error occurs before or during "responseStream = WebResponse.GetResponseStream()")
    Last edited by Philly0494; Jan 3rd, 2010 at 10:48 PM.

  3. #3

    Thread Starter
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: Why the Warning with my 'try catch finally end try' code?

    Quote Originally Posted by Philly0494 View Post
    in your 3rd line try putting this instead of "Dim responseStream As Stream"

    Code:
    Dim responseStream As Stream = Nothing
    you are getting an error because you are performing responseStream.Close() when responseStream may or may not have been initialized (if an error occurs before or during "responseStream = WebResponse.GetResponseStream()")
    thanks philly, that got rid, had been trying with null, case of mixing my languages :s
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width