Venkatesh K
Venkatesh K

Reputation: 4594

Exception handling need to resume called function

In VB.Net,

I am having function x() & function y(). x() calls y().

How can i structure y() code ?

Upvotes: 0

Views: 33

Answers (1)

Matt Wilko
Matt Wilko

Reputation: 27342

You just need to structure your code with a Try Catch Block inside Y like this:

Public Sub X()
    Try
         ### Do some crucial operation here
         Dim obj = Y() 'call Y
         If Not obj Is Nothing Then
             'do some operation on obj if the call to Y succeeded
         End If
         ### Do more crucial operation here - this runs even if Y throws an exception
    Catch ex As Exception
        'x failed for some reason - log the ex.StackTrace and ex.Message
    End Try
End Sub

Public Function Y() As Object
    Try
        Dim obj As New Object
        'do y here
        Return obj
    Catch ex As Exception
        'ignore any error that occurs calling y
        Return Nothing
    End Try
End Sub

Upvotes: 2

Related Questions