none
Substring - Impossible Error RRS feed

  • Question

  • This code generates an unhandled exception when the parameters are valid and also the exception should be handled.

    Two seemingly impossible results. Note that tmpIndex displays as 123, filename has 7 backslashes and total length of 131.

    Dim tmpFilename As String = ECWDoc.Filename
    Dim tmpIndex As Integer = tmpFilename.LastIndexOf("\")
    
    MsgBox("Filename: " & tmpFilename & vbLf & vbLf & "Index of last backslash: " & tmpIndex.ToString("0"), 0, "Debug")
    
    If tmpFilename.ToLower.EndsWith(".dwg") And (tmpIndex >= 0) Then
        Try
            ThisFolder = tmpFilename.Substring(0, tmpIndex) ' generates unhandled exception, length less than zero
        Catch ex As Exception
            MsgBox("Handled exception") ' never gets here
        End Try
    End If

    I recreated the exact path on my computer but cannot duplicate the result which only occurs on someone else's computer.



    • Edited by GTVic Monday, August 14, 2017 7:12 PM
    Monday, August 14, 2017 7:11 PM

Answers

  • Yes, the problem can be solved using different code, e.g. Path.GetDirectoryName, but that wasn't the question. The point of my post is to figure out how a Substring can generate an unhandled exception in this case.

    Edit ....

    The substring was in a different module than reported by the debugger. Once the correct line of code was found then the solution was obvious. I think at least some of my info was incorrect so I'm sorry for the confusion.


    • Edited by GTVic Thursday, October 19, 2017 7:57 PM clarify solution
    • Marked as answer by GTVic Thursday, October 19, 2017 7:57 PM
    Tuesday, August 15, 2017 6:26 AM

All replies

  • GTV,

    Make sure that "tmpFilename" isn't null or empty first.

    Also, it looks like you're dealing with AutoCAD files. Dealing with paths are better left to System.IO.Path:

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


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


    Monday, August 14, 2017 7:21 PM
  • This code generates an unhandled exception when the parameters are valid and also the exception should be handled.

    Two seemingly impossible results. Note that tmpIndex displays as 123, filename has 7 backslashes and total length of 131.

    Dim tmpFilename As String = ECWDoc.Filename
    Dim tmpIndex As Integer = tmpFilename.LastIndexOf("\")
    
    MsgBox("Filename: " & tmpFilename & vbLf & vbLf & "Index of last backslash: " & tmpIndex.ToString("0"), 0, "Debug")
    
    If tmpFilename.ToLower.EndsWith(".dwg") And (tmpIndex >= 0) Then
        Try
            ThisFolder = tmpFilename.Substring(0, tmpIndex) ' generates unhandled exception, length less than zero
        Catch ex As Exception
            MsgBox("Handled exception") ' never gets here
        End Try
    End If

    I recreated the exact path on my computer but cannot duplicate the result which only occurs on someone else's computer.



    Hi

    Don't know what you might be wanting, but here is an example showing how to create a new filename with same path as original. Just ignore if of no use to you.

    ' Blank Form1
    Option Strict On
    Option Explicit On
    Public Class Form1
        Private Sub Form1_Load(sender As System.Object, e As EventArgs) Handles MyBase.Load
    
            ' create a dummy ecwdoc for testing with
            Dim ecwdoc As New doc With {.FileName = "C:\One\Two\Three\Four\filename.dwg"}
    
            Dim ThisFolder As String = IO.Directory.GetParent(ecwdoc.FileName).ToString
    
            ' create a new file with same parent path
            Dim newfile As String = IO.Path.Combine(ThisFolder, "NewFile.dwg")
        End Sub
    
        ' just for testing
        Class doc
            Property FileName As String
        End Class
    End Class


    Regards Les, Livingston, Scotland

    Monday, August 14, 2017 8:10 PM
  • I recreated the exact path on my computer but cannot duplicate the result which only occurs on someone else's computer.

    What is ThisFolder?

    If the code in the Catch block never executes then that indicates the error is occurring somewhere else. That you can't duplicate it with the same folder name indicates the same thing.

    Your test should use shortcircuiting to avoid the case where the name is empty

      If tmpIndex >= 0 AndAlso tmpFilename.ToLower.EndsWith(".dwg") Then

    Monday, August 14, 2017 10:07 PM
  • Hello,

    Try the following.

    Dim tmpFilename As String = "C:\SomeFolder\AnotherFolder\MyFile.dwg"
    
    If IO.Path.GetExtension(tmpFilename).ToLower = ".dwg" AndAlso tmpIndex >= 0 Then
        Try
            ThisFolder = IO.Path.GetDirectoryName(tmpFilename)
            MessageBox.Show(ThisFolder)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End If


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, August 14, 2017 10:37 PM
    Moderator
  • Yes, the problem can be solved using different code, e.g. Path.GetDirectoryName, but that wasn't the question. The point of my post is to figure out how a Substring can generate an unhandled exception in this case.

    Edit ....

    The substring was in a different module than reported by the debugger. Once the correct line of code was found then the solution was obvious. I think at least some of my info was incorrect so I'm sorry for the confusion.


    • Edited by GTVic Thursday, October 19, 2017 7:57 PM clarify solution
    • Marked as answer by GTVic Thursday, October 19, 2017 7:57 PM
    Tuesday, August 15, 2017 6:26 AM
  • Two seemingly impossible results. Note that tmpIndex displays as 123, filename has 7 backslashes and total length of 131

    Hi GTVic,

    What is tmpFilename? At my side, I can not reproduce this issue. You said you would get unhandled exception when length less than zero, what is length? Please help us to confirm this.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, August 15, 2017 6:33 AM
    Moderator
  • Did you also get the stack trace that clearly shows that the exception occurs at the above Substring call, not in other places that maybe use Substring indirectly? For example, put an MsgBox after the End If and check if it is reached.
    • Edited by Viorel_MVP Tuesday, August 15, 2017 6:43 AM
    Tuesday, August 15, 2017 6:42 AM
  • There is no other Substring usage and the unhandled exception states the error is with Substring, specifically that the length parameter is less than zero. My debugging messagebox clearly shows that the length I am trying to extract is 123.

    You need to identify the exact piece of code that is creating the error.  It seems you have proved that it is not the .Substring in the code you posted, but that should be confirmed with additional debug statements, such as log statements before and after.  Then you need to consider where else a substring operation might be occurring. For instance, setting a property of a class will invoke code in the property set routine.  What Type is ThisFolder?

    Tuesday, August 15, 2017 7:06 AM