首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“'On GoTo ErrorHandler”替换为“try catch GoTo finally”块

“'On GoTo ErrorHandler”替换为“try catch GoTo finally”块
EN

Stack Overflow用户
提问于 2017-03-17 10:36:52
回答 1查看 3.4K关注 0票数 0

我已经将VB6项目转换为VB.NET(VS2005)。转换后,我想用try catch块替换'On Error GoTo ErrorHandler‘。请给我一些例子下面的代码“尝试捕捉最终”块。谢谢。

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2017-03-17 11:03:13

它所需要做的就是删除On Error GoTo ErrHandler和您的GoTo本身;但是不要在之后删除代码。另外,删除Exit Function

然后你能做的就是遵循这个格式;

代码语言:javascript
复制
Try
    ' Your first code before ErrHandler:
Catch ex As Exception
    ' Anything in your ErrHandler and replace Err.Description with ex.Message
End Try
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42848254

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档