COSC 1315

Programming Fundamentals

Types of Errors

(Syntax, Runtime, and Logic)

Revised:  January 29, 2007
By Richard G. Baldwin

File:  Pf00120.htm
Practice Text


Preface

This lesson was written specifically for the benefit of my students in COSC 1315, Fundamentals of Programming.  The lesson was written under the assumption that those students have no prior programming knowledge when they enroll in the course.

Another browser window

I recommend that you open another copy of this document in a separate browser window so that you can view the code and the discussion of that code at the same time.

Types of Errors

Three types of errors

There are basically three types of errors that you must contend with when writing computer programs:

Generally speaking, the errors become more difficult to find and fix as you move down the above list.

Syntax errors

In effect, syntax errors represent grammar errors in the use of the programming language.  Common examples are:

Runtime errors

Runtime errors occur when a program with no syntax errors asks the computer to do something that the computer is unable to reliably do.  Common examples are:

There is no way for the compiler to know about these kinds of errors when the program is compiled.

Logic errors

Logic errors occur when there is a design flaw in your program.  Common examples are:

Sample Programs

The three sample programs that follow provide simple examples of all three types of errors.

A syntax error

The program in Listing 1 contains a syntax error that causes the compiler to report a compilation error.  Note that this program is written using an object-based format as described in lesson 110.  Depending on the compiler being used, the error message may look something like the following (or it may look entirely different):

Compiling...
Errors01.cpp
C:\jnk\Errors01.cpp(29) : error C2228: 
left of '.doSomething' 
must have class/struct/union type

/*File:  Errors01.cpp
This c++ program illustrates a syntax error that
produces the following compiler error.  Note that
line breaks were manually inserted into the error
message to force it to fit into this narrow
publication format.

Compiling...
Errors01.cpp
C:\jnk\Errors01.cpp(29) : error C2228: 
left of '.doSomething' 
must have class/struct/union type

************************************************/

#include <iostream>
using namespace std;

class Errors01{ 
  public:
  static void classMain(){
    //Instantiate an object of the Errors01 class 
    // and save its reference in a pointer 
    // variable.
    Errors01* ptrToObject = new Errors01();
    //Now invoke the instance function named 
    // doSomething belonging to the object.
    // ptrToObject -> doSomething();//correct
    ptrToObject.doSomething();//incorrect
  }//End classMain function
  //-------------------------------------------//

  //An instance function of the Errors01 class
  void doSomething(){
    cout << "Hello World\n";
  }//end doSomething function
};//End Errors01 class
//---------------------------------------------//

int main(){
  Errors01::classMain();
  return 0;
}//end main

Listing 1

The cause of the error

The error message is somewhat cryptic.  The actual cause of the compilation error is the attempt on line 29 (shown in boldface) to use a dot operator (.) when the pointer-to-member operator (->) must be used instead.

The pointer-to-member operator (->) must be used to access a member of an object stored in dynamic memory by way of a pointer variable containing the address of the object.  (The correct syntax is shown in a comment in the line immediately above the line with the error.)

Syntax errors are the best kind of errors

Although it is best not to make any programming errors at all,  if you are going to make programming errors, syntax errors are the best kind because they are the easiest to find and fix.

Over the years, compiler developers have worked hard to make compilers smarter so that they can catch errors at compile time that might otherwise turn out to be runtime errors.

A runtime error

Runtime error are usually more difficult to find and fix than syntax errors.

A divide by zero error

The program in Listing 2 produces a runtime error when an attempt is made to divide by a variable containing a value of zero.

Undetectable by the compiler

The compiler is able to detect an attempt to divide by a literal value of zero.

However, the compiler is unable to detect an error resulting from an attempt to divide by a variable with a value of zero.

No way of knowing in advance

In general, the compiler has no way of knowing in advance the value that will be stored in a variable later when the program is run.

(That value could come from anywhere, such as from keyboard input or from a file.)

No compilation errors

The program in Listing 2 compiles with no problems using VS 6.0.  It also compiles with no errors using Dev C++.

/*File:  Errors02.cpp
This C++ program illustrates a runtime error
when an attempt is made to divide a number by
zero.

This program compiles with no problems using
VS 6.0.  However, when the program is run either
from within VS 6.0, or from a command prompt,
a Windows message appears to the effect that
an error has occurred, an error message has
been generated, and permission is requested to
send the error message to Microsoft.

************************************************/

#include <iostream>
using namespace std;

class Errors02{ 
  public:
  static void classMain(){
    //Instantiate an object of the Errors02 class 
    // and save its reference in a pointer 
    // variable.
    Errors02* ptrToObject = new Errors02();
    //Now invoke the instance function named 
    // doSomething belonging to the object.
    ptrToObject -> doSomething();

  }//End classMain function
  //-------------------------------------------//

  //An instance function of the Errors02 class
  void doSomething(){
    int temp1;
    int temp2;
    temp1 = 6;
    //temp2 = 3;//This would be OK
    temp2 = 0;//This causes a runtime error
    cout << temp1/temp2 << endl;
  }//end doSomething function
};//End Errors02 class
//---------------------------------------------//

int main(){
  Errors02::classMain();
  return 0;
}//end main

Listing 2

The runtime error message

When the program is run either from within VS 6.0, or from a command prompt on my machine under WinXP, the error notification shown in Figure 1 appears on the screen.



Figure 1

The nature of the error

Pressing the Debug button causes another box to appear on the screen which reads partially as follows:

Unhandled exception ... Integer Divide by Zero

Thus, the nature, but not the location (line number) of the error can be determined.

The source of the error

The statement that produced the error is shown in boldface in Listing 2.  Although the code contains some elements that we haven't studied in detail yet, the meaning of the code will probably be intuitively obvious to most students.

The program creates two variables named temp1 and temp2 in the function named doSomething.  It stores a value of 6 in temp1 and a value of 0 in temp2.

Then it attempts to use the divide operator (/) to divide temp1 by temp2.  However, because the division of any value by 0 produces an infinite result, the programs terminates with a runtime error.

A logic error

The program in Listing 3 illustrates a simple logic error.  The intent of the program was for the program to display the following text:

Hello World

However, a programming logic error causes the program to display the following text instead:

Goodbye Cruel World

Note that most logic errors are more subtle than this one and are much more difficult to find and fix.

/*File:  Errors03.cpp
This C++ program illustrates a logic error.  The
intent was for the program to display the 
following text:

Hello World

However, a programming logic error causes the
program to display the following text instead:

Goodbye Cruel World

************************************************/

#include <iostream>
using namespace std;

class Errors03{ 
  public:
  static void classMain(){
    //Instantiate an object in dynamic memory
    // and save its reference in a pointer 
    // variable.
    Errors03* ptrToObject = new Errors03();

    //Invoke an instance function on the object.
    ptrToObject -> doSomething();
  }//End classMain function
  //-------------------------------------------//

  //An instance function of the Errors03 class
  void doSomething(){
    cout << "Goodbye Cruel World\n";
  }//end doSomething function
};//End Errors03 class
//---------------------------------------------//

int main(){
  Errors03::classMain();
  return 0;
}//end main

Listing 3


Copyright 2005, Richard G. Baldwin.  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-