Debugging Programs in Visual Cafe

Home Up Search Java 2 API C++ Resources

There a several main ways in which one gets a hold of what's going on in a Java program and track down "undesired features":

  • Breakpoints:  These are lines in the code in where the debugger will stop the normal execution and wait for debugging commands.  Place breakpoints at the beginning of troublesome code sections to gain hold over the program at those points.
  • Single-stepping:  Once the program has been stopped using a breakpoint, the execution can be progressed one line at a time.  This is very useful for making sure that the program progresses as expected.  
  • Watches/Evaluations:  The values of variables and the return values of methods can be checked or monitored.   This is the basic way in which the state of objects can be checked.

Note that there are many more capabilities of the debugger than are discussed here!

Breakpoints:

To set or clear a breakpoint:

  1. Place the cursor on the line where you'd like execution to stop.
  2. Click the "Toggle Breakpoint" button on the main toolbar.

When the program is run, execution will stop every time that line of code is encountered.   At this point, the values of variables and the return values of methods can be checked.   Program execution from that point on can be checked by single-stepping the execution.

Clicking the "Run" button (VCR-like button on main toolbar) will resume normal progam execution.  

It is possible to set a "conditional breakpoint" where execution only halts when a given expression evaluates to true.  To set  a conditional breakpoint, select "Source/Set Conditional Breakpoint" from the main menu.

Tip:  Be very careful that you properly identify which instance of an object you have stopped in.

Tip:  To see what method calls lead up to the present situation, click the "Call Stack" button on the main toolbar.

Back to top

Single Stepping:

There are several modes of single-stepping that can be accessed via three buttons on the main toolbar:

  1. Step Into:  Clicking this button will cause the execution to proceed to the next executable line of code, even if it is in another file, method or object.   Use this mode if you want to trace every detail of what your program is doing.   Note that this will often cause the program to step into the internal code of the built-in Java packages.
  2. Step Over:  Clicking this button will cause the execution to proceed to the next executable line in the method.   If the line is the last line of the method, however,  execution will continue at the next executable line of the calling method.   This mode is very useful for stepping over code that you know already works properly.
  3. Step Out:  Clicking this button will cause the execution to proceed to the next executable line in the method that called the present method.   This mode is useful for to getting out of a method that you already know executes properly.

Use single stepping in conjuction with watches and evaluations to make sure you know exactly what state your objects are in and that the program logic is correct so that execution progresses in the expected manner.

Tip:  Another useful option is to execute normally up to the present cursor location.  Select this option by right clicking the mouse and selecting "Continue to Cursor".   This is helpful to jump over blocks of code.

Back to top

Watches/Evaluations:

A "watch" is a display that, whenever the program execution is halted, shows the value of a variable, the return value of a method, or a more general expression that evaluates to a value. 

To add an expression to the watch list,  click on the "Watch" button on the main toolbar, and type the expression into the next available empty line in the window that appears.   To delete an expression from the watch list, highlight the desired line and press "Delete".

When the program is halted, expressions can be evaluated and their values displayed.  An expression can be a variable, a method or Java code that evaluates to a value.  Note that methods that set values can be evaluated.    Using an expression such as "x=5" can be a handy way of setting an errant variable to its proper value and checking the rest of the program.     Be sure to fix why the variable was incorrect in the first place!

To evaluate an expression, click on the "Evaluate Expression" button on the main toolbar and type in the desired expression.

Tip:  All the property variables of an object can be accessed at once by clicking the "Variables" button on the main toolbar.

Back to top