Learn to Program using Alice

Syntax, Runtime, and Logic Errors

Learn about syntax errors, runtime errors, and logic errors, and some of the ways to avoid them.

Published:  April 4, 2007
Last updated:  June 24, 2007
By Richard G. Baldwin

Alice Programming Notes # 155


Preface

This tutorial lesson is part of a series designed to teach you how to program using the Alice programming environment under the assumption that you have no prior programming knowledge or experience.

Have some fun

Because Alice is an interactive graphic 3D programming environment, it is not only useful for learning how to program, Alice makes learning to program fun.  Therefore, you should be sure to explore the many possibilities for being creative provided by Alice while you are learning to program using these tutorials.  And above all, have fun in the process of learning.

General

In the previous lesson titled "Class-Level Methods and Inheritance" I taught you how to:

In this lesson, I will teach you about syntax errors, runtime errors, and logic errors, as well as some of the ways to avoid them.

Viewing tip

I recommend that you open another copy of this document in a separate browser window and use the following links to easily find and view the figures while you are reading about them.

Supplementary material

Once you have mastered Alice, I recommend that you also study the other lessons in my extensive collection of online programming tutorials.  You will find a consolidated index at www.DickBaldwin.com.

Discussion

There are at least three kinds of errors that you can encounter when writing computer programs:

Syntax errors

Syntax or format errors are a major problem for most beginning programmers using languages such as C++, Java, and C#.  A syntax error occurs when the programmer fails to obey one of the grammar rules of the language.  Typically this involves things like using the wrong case, putting punctuation where it is not supposed to be, failing to put punctuation where it is supposed to be, etc.

One of the great things about Alice as a programming language for beginning programmers is that the drag and drop paradigm used in Alice largely eliminates syntax errors.  With Alice, new programming students are free to concentrate on solving the stated problem without too much worry about how to satisfy the syntax requirements.  Syntax errors probably won't be a problem for you as long as you are programming using Alice.

Runtime errors

A runtime error occurs whenever the program instructs the computer to do something that it is either incapable or unwilling to do.  Since there are a near infinite number of things that fall in this category, there are a near infinite number of ways to write programs that cause runtime errors.

In Alice, runtime errors commonly occur when statements are written in the wrong order, or perhaps the order is modified by dragging statements up and down the screen after they are written.  Another common cause of runtime errors results from the construction of instructions that the computer is unable to carry out.

The classical divide by zero error

The classical way to demonstrate a runtime error is to instruct the computer to divide any number by the value zero.  Any value divided by 0 produces an infinitely large result, which is too large to be accommodated by the computer.  Therefore, in most cases, the computer will tell you that it is unable to perform that operation.

Demonstration

To demonstrate a divide by zero error, create a world, add a penguin to the world, and then construct a statement telling the penguin to move forward by one meter.  Then do the following:

Play the world

In all likelihood, instead of seeing your penguin move as you might have expected, you will see an error message that looks something like Figure 1.

Figure 1. Result of dividing by zero.

In this case, the program instructed the computer to divide the value 1 by the value 0.  The computer responded that it is incapable of performing that operation.

Getting more information

If you would like to know more about what caused the error, you can click the button labeled More Detail>> in Figure 1.  However, it is unlikely that you will understand the explanation of what went wrong, because that explanation is given in terms of the underlying Java language.  A partial sample is shown in Figure 2.

Figure 2. Error message from dividing by zero.
Error during simulation.


Alice version: 2.0 04/05/2005 
os.name: Windows XP 
os.version: 5.1 
os.arch: x86 
java.vm.name: Java HotSpot(TM) Client VM 
java.vm.version: 1.3.1_10-b03 
user.dir: C:\progfile\Alice\Required 

Throwable that caused the error: 
javax.vecmath.SingularMatrixException: cannot invert 
matrix 
at javax.vecmath.Matrix4d.invertGeneral...

...Remaining text deleted... 

If you can understand the error message, you should probably be taking a course using Java3D instead of Alice.

What can you do now?

At this point, you simply need to work through your program disabling statements or blocks of statements, and possibly printing values that are produced as the program executes in an attempt to isolate the statement that caused the error.

Once you isolate the statement that caused the error, you need to try to figure out what you may have done in writing the statement to cause it to produce the runtime error.  Unfortunately, you are pretty much on your own at this point.

Logic errors

Logic errors are usually the most difficult kind of errors to find and fix, because there frequently is no obvious indication of the error.  Usually the program runs successfully.  It simply doesn't behave as it should.  In other words, it simply doesn't produce the correct answers.

The causes of logic errors

Logic errors usually result from one or some combination of the following three causes:

Often found by an irate customer

Unfortunately, logic errors often go undetected until you receive an irate call from your customer telling you that your program caused every employee in the company to receive too much money in their most recent paycheck, or something similarly disastrous.

Demonstration

Consider the following scenario.  You place a penguin in your world and need to cause the penguin to turn RIGHT by 85 degrees.

You drag the penguin.turn tile into the edit pane.  Then you specify RIGHT for the direction and you specify 85 for the amount.

When you play your world, you see that rather than turning right by 85 degrees, the penguin turns 85 complete revolutions.  This is what would happen if you forget that the amount to turn must be specified in revolutions instead of in degrees.  To cause the penguin to turn by 85 degrees, you must specify the amount as 85/360 where 360 degrees represents one complete revolution.

Surely you wouldn't make that mistake...

You may be looking at the above example and telling yourself that you could never make that mistake.  Believe me, you can and you probably will make mistakes similar to that one.  Maybe you won't make that exact mistake, but you will probably make some other mistake that will seem just as preposterous to you once you find the error and fix the program.

How to prevent logic errors

In an attempt to prevent logic errors, you should:

testing, testing, and testing

In Alice, the primary outputs that can be used for testing are:

Devise a comprehensive testing strategy

You should devise a testing strategy that will cover most of the things that can go wrong.  For example, insofar as possible, you should test every method independently of other methods to make certain that each method behaves correctly before combining it with other methods.  If you aren't completely satisfied that a method behaves correctly, don't go forward with that method until you are able to resolve any unanswered questions about the behavior.

Devise a series of tests that you can perform at several milestone levels during the development of the program to confirm that it is behaving properly at each milestone.

Devise a series of tests that you can perform using the final version of the program to confirm that it is behaving properly before delivering it to your customer.

Summary

In this rather short lesson, I taught you about syntax errors, runtime errors, and logic errors and some of the ways to avoid them.

What's next?

From here, we will move on to the topic of expressions and operators.

Lab Project

There is no lab project for this lesson.  The purpose of the lesson is to teach you how to avoid errors.  Thus all of the lab projects in all of the other lessons serve as the lab project for this lesson.

Resources

General resources

Resources from earlier lessons in the series titled "Learn to Program using Alice"

Downloads


Copyright

Copyright 2007, Richard G. Baldwin.  Faculty and staff of public and private non-profit educational institutions are granted a license to reproduce and to use this material for purposes consistent with the teaching process.  This license does not extend to commercial ventures.  Otherwise, reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.

About the author

Richard Baldwin is a college professor (at Austin Community College in Austin, TX) and private consultant whose primary focus is a combination of Java, C#, and XML. In addition to the many platform and/or language independent benefits of Java and C# applications, he believes that a combination of Java, C#, and XML will become the primary driving force in the delivery of structured information on the Web.

Richard has participated in numerous consulting projects and he frequently provides onsite training at the high-tech companies located in and around Austin, Texas.  He is the author of Baldwin's Programming Tutorials, which have gained a worldwide following among experienced and aspiring programmers. He has also published articles in JavaPro magazine.

In addition to his programming expertise, Richard has many years of practical experience in Digital Signal Processing (DSP).  His first job after he earned his Bachelor's degree was doing DSP in the Seismic Research Department of Texas Instruments.  (TI is still a world leader in DSP.)  In the following years, he applied his programming and DSP expertise to other interesting areas including sonar and underwater acoustics.

Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.

Baldwin@DickBaldwin.com

-end-