none
Odd exception RRS feed

  • General discussion

  • Part of the latest project involves some string parsing, the routine looks somewhat like this, processing Web Data and returning only the useful part. It does this as many time as it is told to do, up to over 4000 times. The function is called from a Background Worker.

        Private Function TrimData(DataIN As String) As String
            Dim ReturnString As String = ""
            If DataIN Is Nothing Then ' added later
                Return "No Data"      ' lack of this was the culprit
            End If                    ' but in run mode, it never threw an exception**
            Dim Startpoint As Integer = DataIN.IndexOf("prices" & Chr(34) & ":[{")
            If Startpoint = -1 Then Return ("No Data")
            Dim Endpoint As Integer = DataIN.IndexOf("}]", Startpoint + 1)
            If Endpoint = -1 Then Return ("No Data")
            ReturnString = DataIN.Substring(Startpoint, Endpoint - Startpoint)
            Return ReturnString
        End Function
    
            '**Null reference Exception - Object reference not set to an instance of an object.

    Without the test for nothing, in run mode, the program would just stop processing and show that it was finished, even if it wasn't. This happened randomly but often, but never once threw an exception except finally in debug mode. I added those 3 lines and now the program works as it should every time.

    What I'm wondering is why wouldn't a actual exception be thrown and why would it make the program "finish" when it isn't finished. The background worker does this:

    For each Phrase in a list of phrases
     create a URL from that Phrase 
     Download the contents of the URL
     Send those contents to the TrimDataFunction
     Do some other text manipulation
    Next

    Seems very odd to me...

    Tuesday, May 23, 2017 2:51 AM

All replies

  • Devon,

    I'm not sure but I think that your conditions leave some possibilities not thought out yet - including the fact that an empty string isn't a null string. You might want to use String.IsNullOrWhiteSpace.

    I don't know that's the issue, but it might be worth working through.

    *****

    Consider using a StringBuilder. Not what you asked, but...


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Tuesday, May 23, 2017 3:57 AM
  • What I'm wondering is why wouldn't a actual exception be thrown and why would it make the program "finish" when it isn't finished.

    A code element such as 'DataIN.IndexOf...'  cannot execute if DataIn is Nothing - there is no entry point for the method, and the code cannot continue.  If there wasn't an exception then the error is being masked for some reason, possibly because there is an error handler in effect for the routine, or the current state of the thread prevents the exception from being raised.  Your Event Log might provide a clue.

    Tuesday, May 23, 2017 6:06 AM
  • There is no error handler in that function, or in the background worker. Only in a function that gets a string from a url is there a try catch - it returns "No Data" if there is an exception. I did not find anything in the Application Event Logs for Visual Studio or the app itself.

    Tuesday, May 23, 2017 10:01 PM
  • Devon,

    The only wild guess I can make is that DataIn is a string on a higher level which is used async more times and not synclocked. 

    https://msdn.microsoft.com/en-us/library/ms173179(v=vs.110).aspx


    Success
    Cor

    Tuesday, May 23, 2017 11:13 PM
  • Not that it will many any difference as far as your error goes, you can make the function more definiative. I guess this is the same:

       Private Function TrimData(DataIN As String) As String
            Dim ReturnString As String = "No Data"
    
            If DataIN IsNot Nothing Then ' added later
                Dim Startpoint As Integer = DataIN.IndexOf("prices" & Chr(34) & ":[{")
                If Startpoint > -1 Then
                    Dim Endpoint As Integer = DataIN.IndexOf("}]", Startpoint + 1)
                    If Endpoint > -1 Then
                        ReturnString = DataIN.Substring(Startpoint, Endpoint - Startpoint)
                    End If
                End If
            End If
    
            Return ReturnString
        End Function


    But this has only one path out of the function.

    Is this what they call boxing? When you use the return in the middle of the function it means the compiler has to have a chunk of code hanging.

    Also, if you call the function from a control then when the form initializes it may call the function. So very possible DataIn will not exist or has not been filled.

    You can also add:

           If DataIN IsNot Nothing andalso DataIn.Count > 0  then


           End if

    Just for kicks ie the list has been declared but not filled.

    PS Sorry I guess yours is not a list so more like:

      If DataIN IsNot Nothing andalso not DataIn =""

    Wednesday, May 24, 2017 12:08 AM
  • Tommy-

    I get what you mean about one path, thanks.
    That function is not called until the user does 3 things:
    1 - Load a Configuration file
    2 - Select one or more Items from a listbox populated by loading the Config file
    3 - Click a button that is actually disabled until a Config file is loaded
    So it is surely not called at Load or Show

    Still baffled, program is so far working great. I did add IsNullorWhiteSpace at Frank's suggestion but I had already made 10 runs of 4300 items each and no problems came up.

    Wednesday, May 24, 2017 8:06 PM
  • Tommy-

    I get what you mean about one path, thanks.
    That function is not called until the user does 3 things:
    1 - Load a Configuration file
    2 - Select one or more Items from a listbox populated by loading the Config file
    3 - Click a button that is actually disabled until a Config file is loaded
    So it is surely not called at Load or Show

    Still baffled, program is so far working great. I did add IsNullorWhiteSpace at Frank's suggestion but I had already made 10 runs of 4300 items each and no problems came up.



    Yes, anyway I was thinking it was more like a list when I wrote that etc. One way to find out put a break in the sub and start the app. But I guess its not a start up error.


    I am unsure what the topic is now I guess. You are wondering why you dont get an error message when DataIn is nothing before you added the check?

    I guess you have set the Visual Studio menu Debug - Window - Exceptions to break on all CLR errors? If not then VS wont stop on the error in debug mode.

    Wednesday, May 24, 2017 8:50 PM
  • "I guess you have set the Visual Studio menu Debug - Window - Exceptions to break on all CLR errors? If not then VS wont stop on the error in debug mode."

    Using VS 2010 - I don't see that option. What happened was in run mode (run the exe) it would suddenly stop and report finished, when it wasn't. Finally in debugger mode I saw that exception, added the check for nothing and all is well in both modes. So my question was why wouldn't an exception be thrown when running the executable?

    The background worker basically loops through each selected item, uses the item to get data, cleans up that data and stores it. The function I posted is part of the cleanup. The GetData part is trapped and returns "No Data & ex.message" if there is an exception. The two functions that clean it up aren't trapped.

    Thursday, May 25, 2017 12:40 AM
  • "I guess you have set the Visual Studio menu Debug - Window - Exceptions to break on all CLR errors? If not then VS wont stop on the error in debug mode."

    Using VS 2010 - I don't see that option. What happened was in run mode (run the exe) it would suddenly stop and report finished, when it wasn't. Finally in debugger mode I saw that exception, added the check for nothing and all is well in both modes. So my question was why wouldn't an exception be thrown when running the executable?

    The background worker basically loops through each selected item, uses the item to get data, cleans up that data and stores it. The function I posted is part of the cleanup. The GetData part is trapped and returns "No Data & ex.message" if there is an exception. The two functions that clean it up aren't trapped.

    In VS 2010 it is just the menu Debug- Exceptions.

    If the thrown is not checked you wont get an error in debug for CLR errors.

    Try turning it on, remove the datain = nothing check,  and see if you get an error in debug now.

    I made a small test I still get a no ref error when running the .exe but not when debugging if the clr exceptions is off.

    And if the error hander for some reason does not show the msg (ie take it out) then I get no error running the .exe either.

    Not that it will solve your problem but now you know what the debug- exceptions does and you can see your error in debug and maybe trace it. I get stumped for a while with every new project it seems until I realize break on clr errors is not fully selected for the project by default.

    Public Class Form5
        Private DataIN As String
    
        Private Sub Form5_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Try
    
                CheckBox1.Checked = True
            Catch ex As Exception
                'MsgBox(ex.ToString)
            End Try
    
        End Sub
    
        Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
            Dim t As Integer = Test(DataIN)
    
        End Sub
    
        Private Function Test(theString As String) As Integer
    
            Dim Startpoint As Integer = theString.IndexOf("prices" & Chr(34) & ":[{")
            Return Startpoint
    
        End Function
    End Class


    PS to see it skipping the example error in debug you need to remove the error handler in the load event. Then turn CLR exceptions on and off. When break on CLR exceptions is on it will throw the error on the line it occurs in debug when off it does not. Running the .exe without the error handler you still get a system error either way.


    • Edited by tommytwotrain Thursday, May 25, 2017 12:03 PM used theString in func not datain
    Thursday, May 25, 2017 11:25 AM