[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)
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()")
Re: Why the Warning with my 'try catch finally end try' code?
Quote:
Originally Posted by
Philly0494
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