Vb.net 后台工作程序中未处理的异常
您好,我正在开发的应用程序中进行一些集成测试。导致问题的特定元素是对后台工作程序的调用,后台工作程序询问Oracle数据库。当查询中遇到错误时,我希望异常详细信息将调用堆栈渗透到应用程序级别,并在此时提供适当的用户兼容消息。在示例测试中,基础SQL中存在语法错误,导致OraEx异常:Vb.net 后台工作程序中未处理的异常,vb.net,oracle11g,exception-handling,backgroundworker,Vb.net,Oracle11g,Exception Handling,Backgroundworker,您好,我正在开发的应用程序中进行一些集成测试。导致问题的特定元素是对后台工作程序的调用,后台工作程序询问Oracle数据库。当查询中遇到错误时,我希望异常详细信息将调用堆栈渗透到应用程序级别,并在此时提供适当的用户兼容消息。在示例测试中,基础SQL中存在语法错误,导致OraEx异常: Oracle.DataAccess.Client.OracleException ORA-00907: missing right parenthesis 不幸的是,代码生成以下异常: System.Reflec
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 sub中,尽管我相信我正确地处理了异常。很明显,我在这里遗漏了一些基本的东西,有人能提出一个解决方案吗
提前谢谢
保罗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
下面是后台工作人员执行的代码:
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,检查内部异常以获得更多详细信息(很可能是您希望看到的异常)。当您在事件参数上调用
CountRecords
时,是提前绑定还是延迟绑定?e.Argument
的类型是什么?我怀疑这可能是一个延迟绑定调用,因此异常被包装在TargetInvocationException
中。根据上面的评论,您可以查看targetingException
的InnerException
,看看它是否符合您的预期。@Craig:这是一个迟到的电话。这是一个对象
。伙计们,谢谢你们的反馈。我可以确认这是一个迟到的电话。生成的错误与预期的一样,但是正如我已经指出的,问题在于异常并没有像我预期的那样渗透到调用堆栈中。正在工作人员的DoWork过程中生成targetinvocationexception。我也许应该指出,应用程序允许用户自定义/编辑SQL语句,因此需要进行测试。