| 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. | |||||||||||||||||
Welcome to the ninth chapter of the FREE online programming course: 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). Take a look at this piece of Delphi code, how many possible compile (or logic) errors (not warnings) can you count?
How many? Let's see. There is even one logic error! If you correct all 4 errors in the source, the program will only output one number: 120.
Design time and compile time errors ![]() 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) ![]() 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 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 Run time and Logic 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.
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 yourselfSpend 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 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 >> |
|||||||||||||||||



