Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 461
  • Last Modified:

Programmatically Identify Current Executing Sub/Procedure

In my generic error handlers, I would like to be able to identify the current Procedure/Sub within which an error has occurred.  Is there any programmatic way to obtain the current call stack information or to identify the current sub executing (or perhaps something equivalent to the current line # in the code )?
0
jd576
Asked:
jd576
  • 5
  • 4
  • 2
2 Solutions
 
DabasCommented:
Hi jd576,
VB.NET? C#? VB6?

Dabas
0
 
jd576Author Commented:
VB6
0
 
DabasCommented:
jd576,
This is my own Error Handler, which I have used since VB4

Sub GeneralErrors(SubName As String, ErrNum As Integer)

Dim Msg$, Response As String
    Msg$ = Error$(ErrNum)
    Response = MsgBox("Error: " & Str$(ErrNum) & " " & Msg$ & " in " + SubName + " module. End Program?", vbYesNo + vbCritical)
    If Response = vbYes Then
        End
    End If
 End Sub

Every Function and Procedure starts with
On Error goto Error

And finishes with

Error:
    GeneralErrors "Procedure name goes here", Err.Number
    Resume Next

Dabas
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
DabasCommented:
jd576,
If you also want to include the line where the error ocurred, you will have to number all the lines with a label, or at least those lines that are likely to produce an error.
The variable Erl provides the Line information

You can then change the above to
    GeneralErrors "Procedure name goes here, Line: " & Erl, Err.Number

Dabas
0
 
jd576Author Commented:
I missing something here.  Are you passing SubName, or are you programmatically obtaining SubName?   It is the "procedure name goes here"  that I want to be able to identify and code.   In my present code I have to enter this value manually for each specific sub, then pass it to the error handler, which also writes all error info to a Log file.  When an error occurs, I wish to be able to determine the sub in which it occurs programmatically and then pass it to the error handler -- avoiding rewritting the source sub name manually into each sub as I have been doing.  It is a big program, and I use the log file to track obscure errors that users cannot explain.

I suppose what am asking is this:  are the current SubName and Module available at run time?  How do I call them?  I know err.name, err. number, etc.  It is essentially the type of info that appears in the Call Stack.

Also, when you say set line number with label, can you give an example?  I am not sure how to do this.  

Thanks
0
 
DabasCommented:
jd576,
The answer is no, they are not available at run time. (They are in VB.NET!!!!)
What I have done is created my own AddIn to the VB IDE.
By clicking on an icon on my customized toolbar, I automatically create the On Error Goto Error line, and the lines at the bottom, including the correct sub name.

Dabas
0
 
DabasCommented:
jd576,
> Also, when you say set line number with label, can you give an example?  
> I am not sure how to do this.  

Public Sub MySub

On Error GoTo Error
10      Dim i as Integer
20      For i = 1 to 10
30            Debug.Print i
40      Next i
50      Debug.Print 1

Exit Sub
Error:
      msgbox Erl
End Sub

Dabas
0
 
mmusanteCommented:
Here a commercial solution to your problem (I didn't find a free one sorry) I use it in my company and it saved me a lot of times ...
(http://www.sharewaresoft.com/VBGuard-2000-download-2829.htm)
0
 
mmusanteCommented:
I just found a similar free add-in here (http://www.downloadfreetrial.com/development/deve6022.html) it adds the procedure name but not the line-numbers and it doesn't make a copy of the original project too
0
 
jd576Author Commented:
Thanks.  I will split this one for the correct answer -- NO -- and reference to an add-in solution that seems to work.
0
 
jd576Author Commented:
Thanks.  I will split this one for the correct answer -- NO -- and reference to an add-in solution that seems to work.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now