2.3  Debugging a Script

A nontrivial program has at least one bug – by definition. We purposely introduced one in the example file to give you a chance to fix it.

In this part of the tutorial, we assume you have just completed step 3 in section 2.2.

1.    Select Run | Start command.

The Per Unit calculator main dialog will appear.

2.    Without entering any data, click on “Ohm->PU” button

 A division-by-zero message box will appear

Click on Debug button. The script editor will re-appear in which the line of code where the error occurred is being highlighted in yellow (the last line, at the very bottom).

3.    Select Run | End to stop the execution of the script.

4.    Select the Debug | Toggle Break Point command to set a break point at the error line.

A solid purple dot will appear near the beginning of the line, and the text on the line will be highlighted in purple. This indicates that a break point has been inserted on the line.

5.    Select Run | Start.

Leave “Base kV” edit box blank. Click on “Ohm -> PU” button. The script editor will re-appear showing that the program has stopped at the break point position.

Move the mouse pointer over the BaseZ variable on this line. A small window will appear near the mouse pointer showing current value of this variable, which is zero:

It’s clear that the “divide by zero” error was caused by BaseZ=0 which in turn is caused by the zero value of BaseKV. We will now add some logic to the script program to prevent this from happening.

6.    Select Run | End to stop the program.

7.    Replace the Do …Loop code around the break point by the code shown below:

Do

    button = Dialog( dlg )

    If button = 3 Then Exit Do ' Done

    If dlg.BaseKV <= 0 Then

      Print "Base kV not positive"

    ElseIf dlg.baseMVA <= 0 Then

      Print "Base MVA not positive"

    Else

      BaseZ = dlg.BaseKV * dlg.BaseKV / dlg.BaseMVA

      If button = 1 Then

         dlg.PU = dlg.Ohm / BaseZ

      Else

         dlg.Ohm = dlg.PU * BaseZ

      End If

    End If

Loop

8.    Select Run | Start to run the new code. Click on “Ohm -> PU” with blank Base kV edit box.

A message will appear with a warning:

Press OK. The main program dialog will re-appear, ready to accept new input.

9.    Press Done to close the Per Unit calculator program.