后台工作人员未处理的异常
问题描述:
您好,我正在开发一个应用程序中进行一些集成测试。导致问题的特定元素是调用询问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.
在BackgroundWorker的的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
答
好的,问题解决了。从DoWork中删除了try catch块,并使用e.error将我的异常处理移动到了“RunWorkerCompleted”中。有些阅读文档(RTFM ...)强调了在工作线程中使用Try/Catch会干扰BackgroundWorker的本地功能的事实。再次感谢大家的意见。
Paul,
检查内部异常以获取更多详细信息(很可能是您期望看到的异常)。 – N0Alias
当你在事件参数上调用'CountRecords'时,是早期还是晚期? 'e.Argument'的类型是什么?我怀疑这可能是一个迟到的调用,因此异常被封装在一个'TargetInvocationException'中。根据上面的注释,你可以看看'TargetInvocationException'的'InnerException',看看它是否是你期望的。 – Craig
@克雷格:这是一个迟到的电话。 [**'DoWorkEventArgs.Argument'属性**](https://msdn.microsoft.com/en-us/library/system.componentmodel.doworkeventargs.argument(v = vs.110).aspx)是一个' Object'。 –