【问题标题】:replacement for vb6 resume next替换 vb6 继续下一个
【发布时间】:2018-07-23 21:34:27
【问题描述】:

我有以下代码,我需要用 try catch 块替换 On Error GoTo。但我在 Resume Next 语句中发现的问题。如何在 try catch 块中处理 resume next?

Private Function Add()
    'On Error GoTo PreXmtErr
    bSkipError = True
    Divide(hRequestBlock, "@VIEW-QUALIFIER", 0, sWorkBuffer)
    bSkipError = False

    bSkipError = True
    subtract(hRequestBlock, "INBOUND-@VIEW-ID", 0, sWorkBuffer)
    bSkipError = False

PreXmtErr:             
    If bSkipError = True Then
       bSkipSetItem = True
       Resume Next
    End If
End Function

【问题讨论】:

  • 只需移动 Catch 子句中的第二个代码块,不再需要 bSkipError 变量。
  • Resume Next 无论如何通常都很糟糕。如果您不确切知道发生了什么,那么您如何假设从下一个语句继续进行是安全的?很多时候它会好,但有时它不会,那些时候可能会导致数据丢失或损坏。只是没有办法知道。在 VB.NET 中,您应该只捕获您了解其后果的特定异常。任何意外异常都应由退出或重新启动应用程序的全局异常处理程序负责。

标签: vb.net


【解决方案1】:
'Functions need a datatype
Private Function Add() As Double
    Dim ReturnValue As Double
    Try
        Divide(hRequestBlock, "@VIEW-QUALIFIER", 0, sWorkBuffer)
    Catch ex As Exception
        MessageBox.Show("Divide failed") 'can delete this line but empty catches just swallow errors
    End Try
    Try
        subtract(hRequestBlock, "INBOUND-@VIEW-ID", 0, sWorkBuffer)
    Catch ex As Exception
        MessageBox.Show("subtract failed") 'can delete this line but empty catches just swallow errors
    End Try
    bSkipSetItem = True
    'Functions no longer use FunctionName = to return
    Return ReturnValue
End Function

【讨论】:

  • 感谢您的帮助。
猜你喜欢
  • 2021-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多