【问题标题】:Unhandled Exception in Background Worker后台工作人员中未处理的异常
【发布时间】:2017-03-15 16:34:01
【问题描述】:

您好,我正在开发的应用程序中进行一些集成测试。导致问题的特定元素是对询问 Oracle 数据库的后台工作人员的调用。当查询中遇到错误时,我希望异常详细信息将调用堆栈渗透到应用程序级别,并在此时提供适当的用户兼容消息。在示例测试中,底层 SQL 中存在语法错误,导致 OraEx 异常:

Oracle.DataAccess.Client.OracleException ORA-00907: missing right parenthesis

不幸的是,代码生成了以下异常:

System.Reflection.TargetInvocationException was unhandled
Message: An unhandled exception of type
'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
Additional information: Exception has been thrown by the target of an
invocation.

在后台工作人员的 DoWork 子中,尽管我相信我正在正确处理异常。很明显我在这里遗漏了一些基本的东西,有人可以提出解决方案吗?

提前致谢 保罗·J。

这是调用后台工作人员的代码:

Private Sub EventSearch(ByVal mySQL As String)

    Const procName As String = "EventSearch"

    Try
        _eventMngr = New ScadaEventManager(_CurrentDB, _userName, _myPwd)
        _eventMngr.SQL = mySQL

        'Set the flag and stop query tool status accordingly
        _Stopped = False
        uxStopQueryTool.Enabled = True

        'activate the timer object to ensure that the execute query menu 
        'and tool remain disabled until all background processing is complete
        uxBackWorkTimer.Enabled = True

        _logger.SendLog(Me.Name & "." & procName & " - Scanning for data.", NLog.LogLevel.Trace)
        ReviseStatus(2, "Scanning for data.  Please wait...", Color.Black, True, True)

        'Force the thread to sleep for half a second so the user can see the scanning state taking place
        Threading.Thread.Sleep(500)

        'Launch the background worker to retrieve the required data from the database
        uxBWScan.RunWorkerAsync(_eventMngr)

    Catch ex As Exception

        MsgBox(ex.Message, MsgBoxStyle.Exclamation, My.Application.Info.ProductName)
        _logger.SendLog(ex.Message & ".  Thrown in module " & Me.Name.ToString & "." & procName, NLog.LogLevel.Error, ex)
        Call ResetStatus()

    Finally

    End Try

End Sub

这是后台工作人员执行的代码:

Private Sub uxBWScan_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles uxBWScan.DoWork
    Const procName As String = "uxBWScan_DoWork"
    Try
        e.Argument.CountRecords(_queryTypeID)
        e.Result = e.Argument.RecordCount
    Catch NullEx As NullReferenceException
        _logger.SendLog(NullEx.Message & ".  Thrown in module " & Me.Name.ToString & "." & procName, NLog.LogLevel.Error, NullEx)
        Throw
    Catch OraEx As OracleException
        _logger.SendLog(OraEx.Message & ".  Thrown in module " & Me.Name.ToString & "." & procName, NLog.LogLevel.Error, OraEx)
        Throw
    Finally
    End Try
End Sub

这是产生错误的低级代码:

    Public Sub CountRecords(ByVal queryType As Integer)

        _myDataset = New DataSet
        Try
            _myDataset = _myScadaEventDB.CountRecords(_sqlText)
            If _myDataset.Tables(0).Rows.Count > 0 Then
                If queryType = Enums.QueryType.General Or queryType = Enums.QueryType.KeyPerformanceIndicators Or queryType = Enums.QueryType.TelecontroAnalysis Then
                    _recordCount = _myDataset.Tables(0).Rows(0).Item("RecordCount")
                Else
                    'The query is grouped therefore count the number of records in the table 
                    _recordCount = _myDataset.Tables(0).Rows.Count
                End If
            Else
                _recordCount = 0
            End If
        Catch ex As Exception
            Throw
        Finally

        End Try

    End Sub

【问题讨论】:

  • 检查内部异常以获取更多详细信息(很可能是您期望看到的异常)。
  • 当您在事件参数上调用CountRecords 时,是早期绑定还是后期绑定? e.Argument 的类型是什么?我怀疑这可能是一个后期绑定的调用,因此异常被包装在TargetInvocationException 中。根据上面的评论,您可以查看TargetInvocationException 的InnerException,看看是否符合您的预期。
  • @Craig :这是一个迟到的电话。 DoWorkEventArgs.Argument property 是 Object。
  • 各位,感谢您的反馈。我可以确认这是一个迟到的电话。生成的错误与预期的一样,但是正如我已经说过的那样,问题是异常没有像我预期的那样渗透到调用堆栈中。 targetinvocationexception 正在工作人员的 DoWork 过程中生成。我或许应该指出,该应用程序允许用户自定义/编辑 SQL 语句,因此是测试的原因。

标签: vb.net oracle11g exception-handling backgroundworker


【解决方案1】:

好的,问题解决了。从 DoWork 中删除了 try catch 块,并使用 e.error 将我的异常处理移到了“RunWorkerCompleted”中。阅读一些文档(RTFM ...),强调了在工作线程中使用 Try/Catch 会干扰 BackgroundWorker 的本机功能这一事实。再次感谢大家的意见。

保罗,

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    相关资源
    最近更新 更多