Lesson 03-01: Categories of Errors

Learning Target: I can differentiate between semantic, runtime, and syntax errors.

Types of Errors

In most programming languages, errors can be split up into three different types of categories:

  • Syntax Errors are errors in the way your code is structured. Either there’s a typo or an issue with spacing - in other words, the computer does not understand what the code is trying to do.
  • Runtime Errors are errors that occur when the program is running. This means that there aren’t any syntax errors - we passed that part.
  • Semantic Errors are errors that occur when your program does something that is different from what you wanted it to. These errors aren’t program-stopping errors like the others, and therefore are the most difficult to solve - it usually indicates an issue with the logic of the code.

Some examples of Syntax and Runtime errors include:

  • Syntax Errors
    • Not having a colon after an if statement
    • Not having a tab on the next line after a colon
    • Having = instead of == for equality
    • Not having parentheses after your print statement
    • Forgetting to close a quote or parentheses
  • Runtime Errors
    • Not initializing a variable
    • Trying to divide by zero
    • Trying to modulo by zero
    • Trying to access a string index that doesn’t exist
    • Trying to concatenate a string to a number

How to tell which is which?

It’s easy to tell if something is a semantic error - your code will run without stopping the program, and it will simply do something that you don’t want it to do. Both syntax and runtime errors will stop your code from executing. So how do we differentiate between them?

Syntax errors stop program execution before it even starts. This means that nothing will occur at all. For example, run the following program, which has a valid print statement on line 1, and an invalid statement on line 2:

You can see that there isn’t any output at all, because nothing is run. However, let’s take an example of a runtime error. This time, our first line will have a valid print statement, and our second line will print a variable that doesn’t exist yet.

You’ll notice that the "hi" gets printed before we come across the error. This means that the program execution has started, and therefore, this is a runtime error (an error when the program is running).

Checks For Understanding

Q#1

1
2
3
4
5
num = input("Enter a number: ")
print(num % 3)
print(num % 2)
print(num % 1)
print(num % 0)
    u0301-1: What line in the above code will produce an error?
  • (A) Line 1
  • (B) Line 2
  • (C) Line 3
  • (D) Line 4
  • (E) Line 5
    u0301-2: What type of error is produced from above?
  • (A) Syntax
  • (B) Runtime
  • (C) Semantic

Q#2

1
2
3
4
5
6
y = 10
z = 20
#increment all variables
x = x + 1
y = y + 1
z = z + 1
    u0301-3: What line in the above code will produce an error?
  • (A) Line 1
  • (B) Line 2
  • (C) Line 4
  • (D) Line 5
  • (E) Line 6
    u0301-4: What type of error is produced from above?
  • (A) Syntax
  • (B) Runtime
  • (C) Semantic

Q#1

1
2
3
name = raw_input("Enter your name: )
print("Hello there!")
print("Your name is {}".format(name)
    u0301-5: The above code contains which kinds of errors?
  • (A) Syntax
  • (B) Runtime
  • (C) Semantic
    u0301-6: Which lines contain those errors?
  • (A) Line 1
  • (B) Line 2
  • (C) Line 3

Copy and paste the FIRST line that has an error and correct it so there is no error by adding a single character. Adding more than a single character may throw off the answer detection. Try again!

Copy and paste the SECOND line that has an error and correct it so there is no error by adding a single character. Adding more than a single character may throw off the answer detection. Try again!

Next Section - Lesson 03-02: Python’s Error Messages