VB .NET error handling, pass error to caller

Posted by user1375452 on Stack Overflow See other posts from Stack Overflow or by user1375452
Published on 2013-10-29T15:52:12Z Indexed on 2013/10/29 15:53 UTC
Read the original article Hit count: 254

Filed under:
|

this is my very first project on vb.net and i am now struggling to migrate a vba working add in to a vb.net COM Add-in. I think i'm sort of getting the hang, but error handling has me stymied.

This is a test i've been using to understand the try-catch and how to pass exception to caller

Public Sub test()
    Dim ActWkSh As Excel.Worksheet
    Dim ActRng As Excel.Range
    Dim ActCll As Excel.Range
    Dim sVar01 As String
    Dim iVar01 As Integer
    Dim sVar02 As String
    Dim iVar02 As Integer
    Dim objVar01 As Object

    ActWkSh = Me.Application.ActiveSheet
    ActRng = Me.Application.Selection
    ActCll = Me.Application.ActiveCell
    iVar01 = iVar02 = 1
    sVar01 = CStr(ActCll.Value)
    sVar02 = CStr(ActCll.Offset(1, 0).Value)
    Try
        objVar01 = GetValuesV(sVar01, sVar02)
        'DO SOMETHING HERE
    Catch ex As Exception
        MsgBox("ERRORE: " + ex.Message)
        'LOG ERROR SOMEWHERE
    Finally
        MsgBox("DONE!")
    End Try

End Sub

Private Function GetValuesV(ByVal QryStr As Object, ByVal qryConn As String) As Object

    Dim cnn As Object
    Dim rs As Object

    Try
        cnn = CreateObject("ADODB.Connection")
        cnn.Open(qryConn)
        rs = CreateObject("ADODB.recordset")

        rs = cnn.Execute(QryStr)

        If rs.EOF = False Then
            GetValuesV = rs.GetRows
        Else
            Throw New System.Exception("Query Return Empty Set")
        End If

    Catch ex As Exception
        Throw ex
    Finally
        rs.Close()
        cnn.Close()
    End Try

End Function

i'd like to have the error message up to test, but MsgBox("ERRORE: " + ex.Message) pops out something unexpected (Object variable or With block variable not set) What am i doing wrong here?? Thanks D

© Stack Overflow or respective owner

Related posts about vb.net

Related posts about error-handling