What are the two types of Errors in Computer Programming ?

There are  few common ways to classify types of errors in computer programming. We are going to look at the two most general types of errors.  At the bottom of this post, we do address a third type of error that is sometimes studied.

2 General Categories of Computer Programming Error Types

 

  1. Logic Error – Is a type of bug that has two criteria
    1.  Does not prevent the program from running (or at least starting) . While it is possible that a logic error might, eventually, cause your program to crash, your program will at least start up and begin running with that logic error.
    2. leads to unexpected results.  Below is an animation of a logic error in Penjee. As you can see the program starts correctly but because we turned the wrong way, we end up crashing into a wall–these were unexpected results for the programmer when she first wrote that code. We cover logic errors in depth here .

      Example of a Logic Error
      Example of a Logic Error
  2. Syntax ErrorExactly what it sounds like!  You messed up some language’s syntax . Maybe you forgot to put a colon or a semicolon somewhere in a Python script  or maybe, in Java or C++, you forgot a semicolon .  Pretty much every programming language goes through a parser, and  all syntax errors can be detected by the parser.

Let’s look at some examples of syntax errors. In the first example below, the print  statement would have worked in the old version of Python (python 2), but one of the big changes that came with Python 3 was the new requirement to put parenthesis around the argument.


  1. Semicolons are a syntax error

Syntax errors are much easier to catch because your compiler/interpreter will be able to catch them for you.   In fact, many IDE’s will give you a warning about syntax errors, no need to even run the program.

 

Logic errors, on the other hand, can be quite challenging to diagnose and fix.

Look at the Java code below and try to determine if it’s a logic error or a syntax error.

And the answer is … (scroll down, we didn’t want to give the answer away)

….

 

keep scrolling

….

 

The Syntax is completely valid, but the programmer confused “=” with “==”so it’ s not a syntax error.

 

This would be a semantic error!   Yes, there is a third type of error that many people recognize and this would be an example of one of them!

This particular error is a much harder error to identify than the missing semicolon so we think that there’s a new “Hide and Seek Champion”

New Hide and Seek Champion

 

What about other types of errors?

3. Well, as we just showed, many people include a third error type called a semantic error

Semantic Errors – clearly differ from syntax errors but have some overlap with logic errors.  At the very least, semantic errors often lead to errors in the logic and unexpected results.   Semantic errors involve the meaning of the symbols you wrote. Here’s an example of a semantic error from the pointer based language C:

The first printf will show “123,” while the second will show the memory address of the pointer.  (Ok ok, this is a tough one to understand without having studied a pointer based language, I know. But it’s a classic example of semantic error).

A more accessible example:

A more easy to understand semantic error example is using a reserved word as a variable name. In Python def and yield are both reserved keywords , so the code below is a Python semantic error, but it would be fine in a language like JavaScript because def is not a reserved keyword in JavaScript.

 

What’s the difference between Syntax and Semantic error types ?

Let’s use some analogies with the English language .

The difference between these two classes of errors can be understood by looking at their English language versions. Semantics of course refers to the “meaning of a group of words ” , and syntax , refers to the rules we use to combine the words into meaning.  In English, syntax is the rules of grammar.  It’s possible to string a bunch of words together in a syntactically correct way (ie sticking to the grammar rules), but to create a sentence that has no meaning (fails semantics)

Consider a classic example reproduced below :

Colourless green ideas sleep furiously

– Now, is the quote  above an error in syntax or in semantics ?

The answer is, semantics! Because the syntax (or grammar) of the above words is fine, but when you take all of these words and try to combine them into 1 meaning, you can’t!   That’s a problem in semantics.

Is it really so cut and dry as this analogy?

Unfortunately, the answer is no.   As nice as it is to use an analogy to English, as we try to understand semantics and syntax, there is one big and important difference between  programming languages and human languages : context .

Natural languages have a “context” in which statements are understood.

In English, sometimes domain knowledge is needed to understand the meaning. for instance :

We would assume that the person will not be placed inside a violin case.

But, now consider this example :

This text is ambiguous – a teacher from England teaching history or a teacher teaching English history are equally valid interpretations.

And, computer languages generally are based on “context free  grammars (Read more on that here :  http://stackoverflow.com/questions/6713240/what-is-a-context-free-grammar )

That is, programming languages are  less likely to have ambiguities caused by context, so, with that in mind, you can argue that semantic errors are a subset of logic errors or, at least, are a very small portion of the overall “error pie “.

 

Other ways to classify error types: Compile vs run time

Dynamic vs Static Languages

-Languages that go through a compiler like C or , Java go through a compiler which can catch certain types of errors.   These errors are called compile time errors.  Compile time errors can be syntactical or semantic.  On the other hand, dynamic languages like Python or JavaScript only catch errors at run time.

Compile Time Errors – Any error caught by a compiler. A compiler translates source code into a form that can be understood by a machine . Compilers can catch type errors. So if you declare that a variable , in Java for instance, is an int but then your code treats it as a String. Then the compiler will catch this error and point it out. In fact, this is one of the main reasons that many programmers prefer static languages . Static languages like C++ or Java require you to declare what kind of data a variable or argument is .  Compilers are able to catch all syntax errors and some semantic errors, let’s look at two generic pseudo code examples.

In the above case, a good compiler would catch a divide-by-zero error.  This is a “static semantic error”
In the second case, the compiler would not catch a divide-by-zero because “a” won’t be known until the function is called at runtime. This is a “dynamic semantic error”

Run Time Errors- Errors caught at run time. Dynamic languages like Python do not go through a compiler . Instead, the code is run by an an interpreter. However, even compiled languages like Java end up having run time errors for instance, below is an example of a Java Run Time Error:

In the code above, the compiler doesn’t catch the error that arr[3] does not exist so it’s not until you actually run that code that you learn about the error!

 

If you’re interested in reading more on some of these issues and related topics, here are some interesting discussions on Quora: