|
-
Jan 3rd, 2010, 10:07 PM
#1
Thread Starter
Fanatic Member
[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.
-
Jan 3rd, 2010, 10:40 PM
#2
Hyperactive Member
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.
-
Jan 4th, 2010, 02:26 PM
#3
Thread Starter
Fanatic Member
Re: Why the Warning with my 'try catch finally end try' code?
 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
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|