0

My code updates & inserts into a bunch of tables. About 2 months ago I got the same error as the one below, I assumed it was because the exception it self is Nothing.

So I built in a test to check if it isn't nothing, write the exception details.

However, the error in the screenshot occurred again this morning. My question now is how does a code block like the one below result in the Exception being Null, and in which cases will an exception be Null?

Edited - This is different to this question because I want to know how an EXCEPTION can be 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

I can't actually see what code is triggering the exception, until I can fix the exception handling.

Some more information that might help

  1. I am using asp.net V 4.0
  2. The code is in a class, and the file is located in the appcode directory.
  3. The issue doesn't always trigger
  4. When running the code for a second/third time, the exception doesn't trigger again. So I can't recreate the error all the time.
  5. Windows event viewer didn't log any exceptions for this page.

ANSWER

Found from this question.

An inner exception is the exception that caused the current exception.

It is used in cases when you want to surface a different exception than the one that your code caught but you don't want to throw away the original context.

In order for a new exception to have information about a previous one, like you said, you pass it as constructor parameter to the new one.

Usually, a null inner exception means that the current exception is root cause of the exceptional situation.

Exception Example

Community
  • 1
  • 1
Jacques Koekemoer
  • 1,378
  • 5
  • 25
  • 50
  • I would expect that the issue is here: `If ex.InnerException.ToString <> Nothing`. Why do you have that `ToString` call in there? It serves no useful purpose and, if there is no inner exception, will throw a `NullReferenceException`. – jmcilhinney Mar 07 '17 at 07:46
  • @jmcilhinney I also found that that would be what is causing the exception, but `InnerException` is not supposed to be `Nothing`, the same can be said for `ex.Message` which also throws a `NullReferenceException` sometimes. That's why I am wondering why the `Exception` has nothing in the fields? – Jacques Koekemoer Mar 07 '17 at 07:49
  • `This is different to this question because...` no it´s not. The way locating and fixing a NRE is always the same, thus your question is a dup. – Alex B. Mar 07 '17 at 08:08

1 Answers1

1

How about safeguarding your ex.InnerException? Once you make sure that an InnerException exists then you can access its methods and properties.

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

See this explanation (and read the accepted answer) about InnerException. It is not necessary to have an InnerException, unless the current exception was caused by another exception.

Community
  • 1
  • 1
Giorgos Altanis
  • 2,742
  • 1
  • 13
  • 14
  • The problem with `If ex.InnerException <> Nothing` code is the system then throws the error `Operator '<>' is not defined for types 'System.Exception' and 'System.Exception'.` – Jacques Koekemoer Mar 07 '17 at 07:53
  • Forgive me, it's been some time since I last worked with vb.net, try `Is Not Nothing`. Also see my updated answer for the other part of your question. – Giorgos Altanis Mar 07 '17 at 07:56
  • Your right. Bases on this answer http://stackoverflow.com/a/22826428/1396435 - `Usually, a null inner exception means that the current exception is root cause of the exceptional situation.` – Jacques Koekemoer Mar 07 '17 at 07:59