Solved

Debugging Techniques

Posted on 1998-12-18
9
237 Views
Last Modified: 2010-05-03
I have a couple of questions re debugging a program and finding errors:

First Some Background:

I often use Debug.Print statements throughout the program to help monitor the program flow pattern.  This enables me to quickly narrow down on the code which causes the error.  

I also use an Error Handler Function which traps program errors and displays:
Err.Number
Err.Description
Screen.ActiveForm.Name
Screen.ActiveControl.Name (if one exists)
Several Status Flags *
Approximate Line Number *

*Note: The Status Flags and Line Number displays are determined by various Flag and Line Number variables which are set at critical points throughout the program.

In the error handler I also use a vbYesNoCancel MsgBox to determine the disposition of the error.  I can stop program execution immediately, or return to the Main Program Menu, or try to continue program execution from where the error occurred.

Question 1:
In the development environment, my Debug.Print statements, while extremely useful in tracing program flow, produce hundreds of lines in the Immediate Window.  So each time I run the program I have to manually clear the Immediate Window (by Highlighting and Deleting all lines)
Is there a way to CLEAR THE IMMEDIATE WINDOW by some code at the beginning of the program so that it is always cleared when the program starts running?

Question 2:
Sometimes the error handler gets in the way of tracing the error because it clears the error before you can identify the exact place where the error occurred.  If you go to "Tools - Options - General"  in the development environment, you can click a button which enables a "Break on All Errors" option.  However I cannot use this option because I have some DELIBERATE errors in my program which I use to trigger various choices.  So my question is this: Can you turn the "Break on All Errors" option ON and OFF programatically at various critical points in the program.  If so what is the code?

dougr
0
Comment
Question by:dougr
  • 2
  • 2
  • 2
  • +3
9 Comments
 

Expert Comment

by:janeausten
Comment Utility
this is for the first question, though it's not really an answer...

afaik, there's no way to clear the immediate window thru code.  the best way i know is simply pressing ctrl-a in the immediate window (this automatically selects all), then del.

i do hope someone out there knows an answer to this, i'm very interested in learning it myself.

happy holidays!
0
 
LVL 2

Expert Comment

by:Llandr
Comment Utility
The technique you use in 2 seems a bit confused. Error handlers should not be used to control program flow. Can you develop it further. Otherwise there is the run to this point, CtrlF8 I think.
0
 
LVL 3

Expert Comment

by:vikiing
Comment Utility
Forcing an error is a way to check an error handling routine, but as a rule, it doesn't serve to any other purpose.

¿Why d'you force an error into your program?

0
 

Author Comment

by:dougr
Comment Utility
To: vikiing and Llandr

You are right, forcing an error may just be a sloppy program technique on my part.  I used this technique a lot in the MicroSoft DOS Basic (Professional Edition Version 7.1) where there were multiple modules and overlays and the 640 k memory limit.  I would write an error handling routine in the opening module which caused a default to an opening menu.  Then, no matter how deep into sub-modules, subroutines and overlays I could create a deliberate error (i.e. take the square root of minus 1) to force a return to the opening menu without having to go back through the tree of Subroutines. I would let the program close the Subroutines and close the various open files without having to think about it. (Yes I know - sloppy!).

However, having admitted to sloppy programming, there are still some cases where I use deliberate errors during the development stages of a program.  Sometimes I will write incomplete code segments as I dummy up the program structure in the early stages of a program.  There may be parts of the program that are supposed to open up files which don't exist yet, or to access directories which I haven't yet been created.  In this case I will often use the error which is generated to temporarily bypass that code segment (usually with a message which says "Not Written Yet" or something to that effect).

Also, when supplying a program to a client, certain files and directories are created "on the fly" to the client's specifications, where the program does not know the names in advance.  Sometimes the error flags the non-existence of a required directory and prompts the customer to create it.

Sometimes my development environment is so different from the cutomer's that, for testing purposes, I need to use error handling to allow the program to run on my machine without having to duplicate the files and directory structures which exist on the cutomer's machine.

I guess I may be guilty of sloppy programming by sometimes using error generation as a shortcut to writing more sophisticated code.  However I would still be interested in your comments on my 2nd question.  Does anyone else use these techniques, or am I away out in left field?

dougr

P.S. Does anyone have further comments on my question 1 and on janeausten' addendum to it?
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 3

Expert Comment

by:vikiing
Comment Utility
Regarding q.2: as far as I know, you can't force the flag "Break on all errors" at run time.
 
0
 
LVL 15

Expert Comment

by:Eyal
Comment Utility
Deer dougr
The method you are using is wrong from the basic. You have to build your functions to return boolean, so you can detect if the function you tried to excute done it's perpuse. For Example : Public Function CheckDir(CheckThisDir as String)As Boolean
On Error Goto Err_CheckDir
    if Len(Dir(CheckThisDir))<=0 Then Goto Err_CheckDir
    CheckDir = true
Exit_CheckDir:
    Exit Function
Err_CheckDir:
    CheckDir = False
    Resume Exit_CheckDir
End Function

For general information you asked for...
You can't access any of the IDE from your project.
For accessing the IDE you must be in design time.
0
 
LVL 1

Accepted Solution

by:
peterdc earned 50 total points
Comment Utility
re: q1. You could use debug.print to print 199 blank lines to the intermediate window, which will have the effect of clearing it.
0
 

Author Comment

by:dougr
Comment Utility
To: peterdc

Sory I took so long to evaluate the answer, but Christmas got in the way.

I don't seem to be getting any encouraging answers to my 2 questions, so in the absence of a better way, your idea of clearing 199 lines in the immediate window seems to be a decent compromise on question #1.

Apparently the immediate window can hold approx 200 lines.  Do you know of any to increase this number of lines so that more debug history can be stored?

dougr
0
 
LVL 1

Expert Comment

by:peterdc
Comment Utility
Sorry I don't know of any way to increase this line limit. However if you terminate your debug statements with a semicolon you can make better use of each debug print line. Might not be so easy to read, though there will be a space between each debug print, but at least you are storing more debug history.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now