【问题标题】:How can exception have a NullReferenceException [duplicate]异常怎么会有 NullReferenceException [重复]
【发布时间】: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

在修复异常处理之前,我实际上无法看到触发异常的代码。

更多信息可能会有所帮助

  1. 我正在使用 asp.net V 4.0
  2. 代码在一个类中,文件位于appcode目录下。
  3. 问题并不总是触发
  4. 第二次/第三次运行代码时,异常不会再次触发。所以我不能一直重现错误。
  5. Windows 事件查看器未记录此页面的任何异常。

回答

this question.找到

内部异常是导致当前异常的异常。

当您想要显示不同的异常时使用它 比你的代码捕获但你不想扔掉的那个 原始上下文。

为了让一个新的异常有关于前一个异常的信息, 就像你说的,你把它作为构造函数参数传递给新的。

通常,null 内部异常意味着当前异常是 异常情况的根本原因。

【问题讨论】:

  • 我希望问题在这里:If ex.InnerException.ToString &lt;&gt; Nothing。为什么你有那个ToString 电话在那里?它没有任何用处,如果没有内部异常,将抛出NullReferenceException
  • @jmcilhinney 我还发现这将是导致异常的原因,但InnerException 不应该是Nothing,同样可以说ex.Message 也会抛出一个@ 987654338@ 有时。这就是为什么我想知道为什么 Exception 在字段中什么都没有?
  • This is different to this question because... 不,不是。定位和修复 NRE 的方式始终相同,因此您的问题是一个重复。

标签: asp.net vb.net exception


【解决方案1】:

保护你的 ex.InnerException 怎么样?一旦确定 InnerException 存在,您就可以访问它的方法和属性。

If ex.InnerException Is Not Nothing AndAlso ... Then

关于InnerException,请参阅this explanation(并阅读接受的答案)。不必有 InnerException,除非当前异常是由另一个异常引起的。

【讨论】:

  • If ex.InnerException &lt;&gt; Nothing 代码的问题是系统然后抛出错误Operator '&lt;&gt;' is not defined for types 'System.Exception' and 'System.Exception'.
  • 原谅我,距离我上次使用 vb.net 已经有一段时间了,试试Is Not Nothing。另请参阅我对您问题另一部分的更新答案。
  • 您的权利。基于这个答案stackoverflow.com/a/22826428/1396435 - Usually, a null inner exception means that the current exception is root cause of the exceptional situation.
猜你喜欢
  • 2014-07-01
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多