dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Getting the Line Number on a VB error

  1. #1
    Join Date
    Mar 2001
    Location
    County Durham, England
    Posts
    238

    Getting the Line Number on a VB error

    Hi All,

    Well my project has gone out to the public, 5 have sold, and already problems, I have tried to trap the errors, and its all fine and well getting the error number and description, but getting when its doing it out of the user is a nightmare.

    surely there must be a way in Visual Basic you can get it to return the Line number the error is happening on ?

    Does anybody know how to achieve this ?

    Please, please, please, please help.....

  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726
    Code:
    private sub yoursub()
    
       On error goto ErrHandler
       dim sName as string
       dim iAge as integer 
    
    1:  sName ="mario"
    2:  iAge =sname
    3:  msgbox "No mistake ? Or did you see a line number?"
    
    ErrHandler:
    msgbox err.description & vbcrlf & " Line of error:" & Erl 
    resume 3
    end sub
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  3. #3
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726
    (note: you can also skip the ":" after the number...)
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  4. #4
    Join Date
    Mar 2001
    Location
    County Durham, England
    Posts
    238
    Cheers Cimperiali

    I had found that but just kept getting line 0

    Do I have to type all the line numbers in down the left hand side, or can they be put in automatically ?

    Much appreciated

    Ken

  5. #5
    Join Date
    Dec 2001
    Posts
    6,332
    Fantastic!

    Sure wish I knew about this earlier...

    Yes, you do need line numbers for every line, at least those which you might suspect are causing problems. This is because the compiled exe doesn't consist of lines of code, so it really can't tell you unless there is something that becomes part of the compiled version.

    I just did a simple test:
    Code:
    Private Sub Command1_Click()
    Dim A$, I&
    On Error Resume Next
    5 A = "hello"
    ThisSpot: I = A
    If Err Then Debug.Print Erl
    End Sub
    The number returned is 5, even though that line didn't cause an error. Seems it will return the last line number before the error occures.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  6. #6
    Join Date
    Jun 2002
    Location
    Clane, Ireland
    Posts
    766
    The ERL will only return the last line number it encountered, so it means you have to drop it into places where you think there may be an error.

    Another thing I found useful was to set up conditional debugging. In my program I have code that says

    Code:
    IF COMMAND$ = "D3BUG" THEN
        MSGBOX .....
    ENDIF
    I can set up a short cut to the exe which contains D3BUG after the application name. I misspell it, so that people don't generally know its there.

    HTH
    JP

    Please remember to rate all postings.

  7. #7
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    As we are into it...write to a log file.

    Another useful tecnique is to open a (txt) file for append
    and write some infos in it
    (like "Stepped in this sub", "found this value", "raised this error"...)
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)