-1

i'm looking for one way to print the error line when the error handler are launched. Exist some way to get the Error line and the function name to print it ?

Beny Hdez
  • 92
  • 1
  • 8
  • See http://stackoverflow.com/q/23945321/11683 – GSerg May 16 '17 at 19:36
  • 4
    You can use the erl command to get the line number of the last error. However, it does require that your source code have line numbers. – Brian M Stafford May 16 '17 at 20:19
  • Possible duplicate of [what is the better way to handle errors in VB6](http://stackoverflow.com/questions/116289/what-is-the-better-way-to-handle-errors-in-vb6) – StayOnTarget May 17 '17 at 11:30

2 Answers2

2

You can use an Error Handler:

Public Function DoSomething() As Boolean
    On Error GoTo ERR_HANDLER

    ' your code

    DoSomething = True
    Exit Function

ERR_HANDLER:
    MsgBox Err.Description & vbCrLf & "on DoSomething procedure"
    If App.LogMode = 0 Then ' execute for IDE-DEBUG only (*)
        Stop
        Resume
    Else  ' App.LogMode = 1
       ' write error into a LOG file (optional)
    End If
End Function

(*) This allows to break on errors and check code and variables in 'real-time' I use always this method on my code, it's very useful.

Suggestion: You can automate writing this Error Handler using MZTools 8.x add-in (both for VB6/NET) Unfortunately, version 8.x isn't longer freeware, as version 3.x (specific for VB6) BUT, fortunately, you can download version 3 for Wayback Machine

MZ-Tools 3.00.1212 released (October 10, 2013)

https://web.archive.org/web/20150206030204/http://mztools.com:80/v3/download.aspx

Giorgio Brausi
  • 414
  • 3
  • 7
2

Primordial BASIC interpreters required that every line had to start with a unique number. You could learn them by heart, mistype a line number, and make a total mess out of your program. Meta commands like AUTO were used to automate this tedious work. Spacings of 10 between line numbers were common, so new lines could be inserted without the need to shift all numbers below.

The interpreter runtime tracked the line number of the currently executing statement. It was stored in a special variable, sometimes accessible with a reserved keyword, sometimes accessible with absurd kludges.

With Microsoft Visual Basic, this requirement became - and this is a little known fact even among seasoned VB6 developers - an option. There are complications, because VB6 has a syntax for line continuation (the _ character), which asks for gaps in line numbers since it is only an editor convention and treated like a single line by the interpreter.

Developers could make use of that information in error handlers. The reserved keyword Erl stores the number of the line that caused the ON ERROR jump:

Print Erl '"error line"

When you try that in VB6, you will find that it is always zero, unless you assign a unique line number to every statement in the current SUB or FUNCTION.

1000    Private Sub Test()
1010    On Error Goto Err1
1020        Dim I As Long, _ 'see how the line continuation has just one line #
                J as Long
1030        I = 1
1040        J = 0
1050        Print I / J
1060    On Error Goto 0
1070        'succeeded (unlikely)
1080        Exit Sub
1090    Err1:
1100        Print Erl 'will print 1050
1110    End Sub

Why is that useful?

Err... well, sometimes, there may be legacy applications still in full business critical production use by clients (*shush*) that throw errors that just cannot be reproduced in the unsupported legacy development environment on Windows XP Home Edition. So we use the always verbose text of the error message to identify the causing code module, quickly add those line numbers, patch the poor client, and get back with the line number of some year2k bug from a developer long since retired.

There is no concept in VB6 analoguous to reflection or the program debug databases of VB.NET, so the only known way to include information about the call stack is to repeat the name of the executing procedure as a string literal, manually or using a tool, just as shown in the accepted answer.

Cee McSharpface
  • 8,493
  • 3
  • 36
  • 77