1. Home
  2. Computing & Technology
  3. Delphi Programming
Cleaning your Delphi code errors
A discussion on Delphi design, run and compile time errors and how to prevent them. Also, a look at some solutions to the most common logic errors.
 More of this Feature
Printer friendly versionPrinter friendly version
 Join the Discussion
"Post your views and comments to this chapter of the free Delphi Programming Course"
Discuss!
 Related Resources
• A Beginner's Guide to Delphi Programming.TOC

• Preventing Access Violations in Delphi code
• Before you start optimizing Delphi code
• Speed and Size: Top Tricks
• Coding standards
• Code Pascal Faster

• Book: Developers Guide to Delphi Troubleshooting

Welcome to the ninth chapter of the FREE online programming course:
A Beginner’s Guide to Delphi Programming.
A discussion on Delphi design, run and compile time errors and how to prevent them. Also, take a look at some solutions to most common logic errors.

Yes, even the experienced Delphi programmers make mistakes
It happens to me all the time, never mind my 6+ years of Delphi programming experience. Compiler errors like "Missing operator or semicolon" or ":= expected but = found", after pressing the F9 key (run command from Delphi IDE), constantly pop up!

Suppose you are writing a recursive procedure to calculate the factorial value of an integer. The procedure should also output each iteration value to a screen in a console mode Delphi application.

Below you can find one solution to the problem - to calculate 5! (=120).
The output should look like this:
1
2
6
24
120

Take a look at this piece of Delphi code, how many possible compile (or logic) errors (not warnings) can you count?

[line 0] procedura ErrorTesting;
[line 1] var
[line 2]   i, k : integer; j : string;
[line 3] begin
[line 4]   j := 1
[line 5]   i := 5;
[line 6]   for k:= 1 to i do
[line 7]     j = k * j;
[line 8]   writeln(j);
[line 9] end;

How many?
I'm counting four. Would you believe, in less then 7 lines of Delphi code there are 4 errors - and on the first look it seems quite ok!

Let's see.
Line 0: Error! Declaration expected but identifier 'procedura' found.
Line 4: Error! Incompatible types: 'String' and 'Integer'.
Line 5: Error! Missing operator or semicolon.
Line 7: Error! := expected but = found

There is even one logic error! If you correct all 4 errors in the source, the program will only output one number: 120.

Errors and how to prevent them
In general, there are four types of errors a Delphi programmer can experience while developing, even the simplest, Delphi application.
Design and compile time errors are seen only by a developer, while run time and logic errors tend to crash your program right in front of the user's eyes! The good thing about design and run time errors is that Delphi will not let you build (compile) the project - therefore stopping you deploying a non working version of the application. The bad thing about the run time and logic errors is that Delphi will build the exe for you - but that exe might or might not work properly.

Design time and compile time errors
Fighting against these two is, fortunately, fairly easy.
An example. Setting the TabOrder property of any control on a form to more than 32767 will result in a design time error. A message box will pop up telling you what the boundaries are for the property values.

Design time Delphi error

In this case you do not need to reach for Delphi Help in order to understand what's causing the error - and how to fix it.

On the other hand, compiler errors will prevent you from building the exe for the application. When you press the F9 key (in order to build and run the project) and there are some errors in the code, Delphi will stop the compile process and display the errors inside the Messages window (in most cases under the Code Editor window)

Compile time Delphi error

If you know how to correct the particular error, you simply go to the line of code the Delphi compiler reported and fix it. However, if you are unsure about some of the errors (that is how to fix them), you should select that error inside the Messages windows and press F1 (Help). Many times, the error description will provide enough information for you to be able to correct your code.

In the case of ':= expected but = found', what we wanted to do is to assign the value of the right side (k * j) of the = sign to the variable (j) on the left. By mistake, we tried to do a logical operation '=' on the expression. Simply, changing '=' to ':=' solves the problem.

How about 'Missing operator or semicolon'? Delphi warns that the error is in line 5 (code above). I'm pretty sure that in
[line 5] i := 5;
there is no missing operator!

Ah, yes. We have the missing ';' at the end of line 4. But why did Delphi stated that the error is in line 5? Remember that, in Pascal language, each line of code must end with the ';' sign. In the code above, Delphi compiler actually sees
j := 1
i := 5;
(two lines) as (one line)
j := 1 i := 5;
Therefore, a semicolon is often missing on the previous line!

Run time and Logic errors
Fighting against these two is, unfortunately, a fairly hard task.
Firstly, you'll not know about those errors until the users of your application report them. Secondly, you'll be truly happy if those users succeed in explaining why your program stopped working - in order for you to be able to recreate the situation and correct the code.
For example, suppose you are coding a data aware application. The program lets the user to specify the location of the database your code needs to connect to. If you have not used try/except statements to handle exceptions, your code might try to open a non-existing database - resulting in a run time error.
The "Preventing Access Violations in Delphi code" article provides a more in-depth discussion on run time errors.

Code logic errors. Oh those nasty logic errors. They can go unnoticed indefinitely, or more scarily, they can go unnoticed indefinitely and really mess with the users data (for example, in an, accounting application).

Of course there is a logic error in the code above - the output will only contain one line : the number 120 - where we need 5 lines, for each iteration of the for loop. Where is the problem?

The problem "lies" in the for..do Delphi statement. In Delphi the for loop is defined as follows:

for counter := initialValue to finalValue do statement

The most tricky part is the statement "parameter". Statement can be a simple or structured statement that does not change the value of counter. Structured statements are built from other statements. If the structured statement consists only of simple statements (that is more than one) it MUST be surrounded with the begin / end pair.
You, as a beginner developer, would most likely run into problems with using the for loop with a structured statement as if it were a simple statement.
Therefore our for loop should look like this:

...
for k:= 1 to i do
begin
  j = k * j;
  writeln(j);
end;
end; //procedure

Note: A Suggestion. ALWAYS, always write Delphi commands that expect a simple OR a structured statement like they expect a structured statement!! In other words, if you have a for loop with only one simple statement inside its body, DO surround that line with the begin / end pair. You'll be more than happy, when you go back to your code to alter it, and realize there is no need to carefully look where your for loop begins and where it ends - and even more so if you have nested for / while / try-except and similar blocks!

Help yourself
Spend a few bucks and buy a book! "The Tomes of Delphi Developer's Guide to Delphi Troubleshooting", by Clay Shannon, covers all versions of Delphi. This book is an alphabetical reference to nearly 800 design-time, compile-time, and run-time error messages of Borland’s Delphi Object Pascal language. Error message entries include an explanation of what actions or omissions may have caused the error message, how to quickly resolve the problem, and how to avoid receiving the error message in the future. Code samples clearly demonstrate how to eliminate the errors in different versions of Delphi.

   To the next chapter: A Beginner's Guide to Delphi Programming
This is the end of the ninth chapter, in the next chapter, I'll show you how to build a real game using Delphi

If you need any kind of help at this point, please post to the Delphi Programming Forum where all the questions are answered and beginners are treated as experts.

A Beginner's Guide to Delphi Programming: Next Chapter >>
>> Your First Delphi Game: Tic Tac Toe

Explore Delphi Programming

More from About.com

  1. Home
  2. Computing & Technology
  3. Delphi Programming

©2008 About.com, a part of The New York Times Company.

All rights reserved.