I call a method that may throw an exception from within an overrided function. If MyBase.GetWebResponse()
throws an exception, how could I catch it from main code? Debugger stops in GetWebResponse
, not in the main Try
.
I use the overrided function in order to get redirected ResponseUri. Without using GetWebResponse
, I can catch exceptions.
Main code:
Try
RetryAction(Sub()
Using cawc As New CookieAwareWebClient)
strContent = cawc.DownloadString(m_url)
m_url = cawc.ResponseUri.ToString
End Using
End Sub, 10, 1000)
Catch ex As WebException
StreamNotAvailable(ex)
End Try
Function:
Public Sub RetryAction(action As Action, numRetries As Integer, retryTimeout As Integer)
If action Is Nothing Then
Throw New ArgumentNullException("action")
End If
' slightly safer...
Do
Try
action()
Exit Sub
Catch
If numRetries <= 0 Then
Throw
Else
' improved to avoid silent failure
Thread.Sleep(retryTimeout)
End If
End Try
Loop While Interlocked.Decrement(numRetries) > -1
End Sub
Class:
Public Class CookieAwareWebClient
Inherits WebClient
...
Private m_ResponseUri As Uri
Public ReadOnly Property ResponseUri() As Uri
Get
Return m_ResponseUri
End Get
End Property
Protected Overrides Function GetWebResponse(request As WebRequest) As WebResponse
Dim response As HttpWebResponse = MyBase.GetWebResponse(request)
m_ResponseUri = response.ResponseUri
Return response
End Function
End Class
I finally found the solution:
Try
RetryAction(Sub()
Try
Using cawc As New CookieAwareWebClient)
strContent = cawc.DownloadString(m_url)
m_url = cawc.ResponseUri.ToString
End Using
Catch ex As Exception
Throw
End Try
End Sub, 10, 1000)
Catch ex As WebException
StreamNotAvailable(ex)
End Try
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.