'On Error GoTo ErrorHandler'替换为'try catch and finally'块

时间:2017-03-17 02:36:52

标签: vb.net vb6 vb6-migration

我已将VB6项目转换为VB.NET(VS2005)。转换后,我想用try catch块替换'On Error GoTo ErrorHandler'。请给我一些下面的代码示例“try catch finally”块。谢谢。

Private Sub GetMktAccessRights2(ByRef jsonObj As Scripting.Dictionary, ByRef nCount As Short)
    On Error GoTo ErrorHandler
    Dim i As Short
    Dim ctr As Short

    If nCount = 0 Then
        WriteErrorToTextFile(ErrorFileName, "Cannot get market access rights description.")
        InsertDealerErrorLog("FrmMain: GetMktAccessRights(): Cannot get market access rights description.")

        MsgBox("Cannot get market access rights description.", MsgBoxStyle.OKOnly)
    Else
        NumOfExch = 0
        For i = 1 To nCount
            'UPGRADE_WARNING: Couldn't resolve default property of object jsonObj.Item().Item. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            If (AccessRights And jsonObj.Item("records").Item(i).Item("DealerRights")) = jsonObj.Item("records").Item(i).Item("DealerRights") Then
                NumOfExch = NumOfExch + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object jsonObj.Item().Item. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                CFD_data(NumOfExch - 1).Market = Trim(jsonObj.Item("records").Item(i).Item("Market"))
            End If
        Next 
        If NumOfExch = 0 Then
            InsertDealerErrorLog("FrmMain: GetMktAccessRights(): You cannot access any of the markets.")
            MsgBox("You cannot access any of the markets because you are not assign access to the different markets.", MsgBoxStyle.OKOnly)
        End If
    End If

    Exit Sub
ErrorHandler: 
    MsgBox("GetMktAccessRights2 Error. Maybe response message error.")
    Resume Next

End Sub

Private Function SendDealerInfoToRskMng(ByVal Loginout As Boolean, ByVal DealerID As String, ByVal MarketList As String, ByVal ListidList As String) As Boolean
    On Error GoTo ErrHandler

    WriteErrorToTextFile(LogFileName, "Begin to SendDealerInfoToRskMng.")

    Dim logindata As DealerLoginData
    logindata = New DealerLoginData

    Dim strDate As String
    strDate = VB6.Format(Now, "yyyy-mm-dd hh:MM:ss")

    tcpendpoint.SendOut(logindata.handle)

    SendDealerInfoToRskMng = True

    'UPGRADE_NOTE: Object logindata may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
    logindata = Nothing

    WriteErrorToTextFile(LogFileName, "Success to SendDealerInfoToRskMng.")
    Exit Function
ErrHandler: 
    MsgBox("PMEModule:SendDealerInfoToRskMng()->" & Err.Description)
    InsertDealerErrorLog("PMEModule:SendDealerInfoToRskMng()->" & Err.Description)
    End

End Function

1 个答案:

答案 0 :(得分:3)

只需删除On Error GoTo ErrHandler和GoTo本身;但是之后不要删除代码。另外,请删除Exit Function

你可以做的就是遵循这种格式;

Try
    ' Your first code before ErrHandler:
Catch ex As Exception
    ' Anything in your ErrHandler and replace Err.Description with ex.Message
End Try