Reputation: 9555
In the code below I have nested functions / subs
What is meant to happen is if there is an exception then stop wherever the code is and add the exception to a global variable and go to the line below 'If there was an exception then go here'
How do I do this as my code below is not working? surely this has to be easy?
Below if the exception happens in Inner_B() then it gets caught in Inner_A() but I want tit caught in Inner_B()
Partial Class _Default
Inherits System.Web.UI.Page
Dim textOutput As String = String.Empty
Dim strErrorMesssage As String = String.Empty
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Outer()
'If there was an exception then go here'
End Sub
Private Function Outer() As String
Try
Inner_A()
Catch ex As Exception
strErrorMesssage = ex.Message
Throw
End Try
textOutput &= " Function Outer"
Return " Function Outer"
End Function
Private Function Inner_A() As String
Try
Inner_B()
Catch ex As Exception
strErrorMesssage = ex.Message & " Inner_A() "
Throw
End Try
textOutput &= " Function Inner_A"
Return " Function Inner_A"
End Function
Private Sub Inner_B()
Throw New ApplicationException("Exception Occured")
Try
Inner_C()
Catch ex As Exception
strErrorMesssage = ex.Message & " Inner_B() "
Throw
End Try
textOutput &= " Sub Inner_B"
End Sub
Private Function Inner_C() As String
Try
Catch ex As Exception
strErrorMesssage = ex.Message & " Inner_C() "
Throw
End Try
textOutput &= " Function Inner_C"
Return "Inner_C"
End Function
End Class
Upvotes: 0
Views: 87
Reputation: 11792
I'm guessing at what you mean by 'not working', but I assume you're not getting to execute code after the call to Outer()
?
Just wrap it in a Try Catch as well but don't re-throw the exception if you want to conintue anyway.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
Outer()
Catch ex As Exception
'do something with it here if you want, or not.
End Try
'Do more here.
End Sub
Upvotes: 2