Lately I found a bug in my program (or a wrong assumption). I use a function to read data from a web location, via http.
Function ReadItemsFromWeb() As Boolean
Try
Dim uri As New System.Uri(My.Settings.sCCTBWebLocation)
Dim request As Net.WebRequest = Net.WebRequest.Create(uri)
request.Proxy = Net.WebRequest.DefaultWebProxy
request.Credentials = Net.CredentialCache.DefaultCredentials
If Not request.GetResponse Is Nothing Then
Dim response As Net.HttpWebResponse = CType(request.GetResponse(), Net.HttpWebResponse)
Dim dataStream As IO.Stream = response.GetResponseStream()
Dim reader As New IO.StreamReader(dataStream)
Dim responseFromServer As String = reader.ReadToEnd()
If Not responseFromServer.Trim = "" Then
My.Settings.sSettingCCTBNewItems = responseFromServer
My.Settings.iSettingCCTBLastCheck = DateSince()
My.Settings.Save()
End If
response.Close()
reader.Close()
dataStream.Close()
request = Nothing
Return True
End If
Catch ex as Exception
End Try
Return False
End Function
I thought the Catch would catch every error, because Exception is generic. But the function resulted in an error when there was a proxy error, or no internet connection. The function did work when I catched the ex as Net.WebException.
Why did the Catch ex as Exception not catch the WebException?
I also can't use this, because a WebException is not an ordinary Exception:
Catch ex As Exception
If TypeOf ex Is Net.WebException Then
End If
There is no general way to catch every exception? Or is this an exception where I have to catch every possible exceptions (WebException, HttpListenerException, CookieException)?