【发布时间】:2017-07-27 08:05:59
【问题描述】:
我的代码更新并插入到一堆表中。大约 2 个月前,我遇到了与以下错误相同的错误,我认为这是因为它自身的异常是 Nothing。
所以我建立了一个测试来检查它是否不是什么,写下异常细节。
然而,截图中的错误今天早上又出现了。我现在的问题是,像下面这样的代码块如何导致异常为Null,在哪些情况下异常为Null?
已编辑 - 这与 this question 不同,因为我想知道 EXCEPTION 如何是 Null/Nothing
Try
If t.DpaPosted = "N" Then
i += 1
OutputCount += 1
Dim SubAccountID As Integer = 0
If t.SubaccountId = 0 Then
SubAccountID = GetSubAccountID(t.DpaInId)
If SubAccountID = 0 Then
If DupAcc > 1 Then
UpdateLog(t.clientReference & " - Multiple CLOSED(Paid Up/Refunds outstanding) accounts.")
Else
SubAccountID = GetClosedSubAccountID(t.DpaInID)
If SubAccountID = 0 Then
UpdateLog(t.clientReference & " - No accounts found.")
End If
End If
End If
End If
If SubAccountID <> 0 Then
If t.DpaDocNo <> "0" Then
If q.Execquery("SELECT COUNT(1) FROM Transactions WHERE SubAccountID = " & t.SubaccountId & " AND TranTypeID = 6 AND TranAmount = " & t.DpaAmount & " AND DPANo = '" & t.DpaDocNo & "'") > 0 Then
HttpContext.Current.Response.Write("Sequence [" & t.DpaDocNo & "] already posted.<br/>")
dpaadapter.SetDpaPosted(t.DpaInId)
Else
PostDpa(SubAccountID, 6, t.DpaAmount, t.Dpadate, "0", t.DpaDocNo)
dpaadapter.SetDpaPosted(t.DpaInId)
End If
Else
PostDpa(SubAccountID, 6, t.DpaAmount, t.Dpadate, "0", t.DpaDocNo)
dpaadapter.SetDpaPosted(t.DpaInId)
End If
End If
If WriteOutput Then HttpContext.Current.Response.Write("Imported [" + i.ToString + "] of [" + dpa.Rows.Count.ToString + "]<br/>")
If OutputCount = 10 Then
OutputCount = 0
If WriteOutput Then
If HttpContext.Current.Response.IsClientConnected Then
HttpContext.Current.Response.Flush()
HttpContext.Current.Response.Write("<script>window.scrollTo(0,document.body.scrollHeight);</script>")
HttpContext.Current.Response.Flush()
End If
End If
End If
End If
Catch ex As Exception
Dim LogStr As String = ""
LogStr = "Record [" & i.ToString & "]. "
LogStr = "Ref [" & t.clientReference & "] - "
If ex.Message.ToString <> Nothing Then LogStr = LogStr & ex.Message.ToString Else LogStr = LogStr & "No Exception Message"
If ex.InnerException.ToString <> Nothing Then LogStr = LogStr & vbCrLf & ex.InnerException.ToString Else LogStr = LogStr & "No Inner Exception"
UpdateLog(LogStr)
SaveLog()
End Try
在修复异常处理之前,我实际上无法看到触发异常的代码。
更多信息可能会有所帮助
- 我正在使用 asp.net V 4.0
- 代码在一个类中,文件位于
appcode目录下。 - 问题并不总是触发
- 第二次/第三次运行代码时,异常不会再次触发。所以我不能一直重现错误。
- Windows 事件查看器未记录此页面的任何异常。
回答
内部异常是导致当前异常的异常。
当您想要显示不同的异常时使用它 比你的代码捕获但你不想扔掉的那个 原始上下文。
为了让一个新的异常有关于前一个异常的信息, 就像你说的,你把它作为构造函数参数传递给新的。
通常,null 内部异常意味着当前异常是 异常情况的根本原因。
【问题讨论】:
-
我希望问题在这里:
If ex.InnerException.ToString <> Nothing。为什么你有那个ToString电话在那里?它没有任何用处,如果没有内部异常,将抛出NullReferenceException。 -
@jmcilhinney 我还发现这将是导致异常的原因,但
InnerException不应该是Nothing,同样可以说ex.Message也会抛出一个@ 987654338@ 有时。这就是为什么我想知道为什么Exception在字段中什么都没有? -
This is different to this question because...不,不是。定位和修复 NRE 的方式始终相同,因此您的问题是一个重复。