Solved

Programmatically Identify Current Executing Sub/Procedure

Posted on 2004-03-23
11
439 Views
Last Modified: 2013-12-25
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
Comment
Question by:jd576
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 27

Expert Comment

by:Dabas
ID: 10663927
Hi jd576,
VB.NET? C#? VB6?

Dabas
0
 

Author Comment

by:jd576
ID: 10664460
VB6
0
 
LVL 27

Expert Comment

by:Dabas
ID: 10664527
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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 27

Expert Comment

by:Dabas
ID: 10664542
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
 

Author Comment

by:jd576
ID: 10664638
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
 
LVL 27

Assisted Solution

by:Dabas
Dabas earned 125 total points
ID: 10664659
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
 
LVL 27

Expert Comment

by:Dabas
ID: 10664669
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
 
LVL 6

Expert Comment

by:mmusante
ID: 10665204
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
 
LVL 6

Accepted Solution

by:
mmusante earned 125 total points
ID: 10665285
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
 

Author Comment

by:jd576
ID: 10673399
Thanks.  I will split this one for the correct answer -- NO -- and reference to an add-in solution that seems to work.
0
 

Author Comment

by:jd576
ID: 10673400
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question