Skip to main content

Foundations of Python Programming: Functions First

Section 3.4 Runtime Errors

The second type of error is a runtime error. A program with a runtime error is one that passed the interpreter’s syntax checks, and started to execute. However, during the execution of one of the statements in the program, an error occurred that caused the interpreter to stop executing the program and display an error message. Runtime errors are also called exceptions because they usually indicate that something exceptional (and bad) has happened.
Here are some examples of common runtime errors you are sure to encounter:
  • Misspelled or incorrectly capitalized variable and function names
  • Attempts to perform operations (such as math operations) on data of the wrong type (ex. attempting to subtract two variables that hold string values)
  • Dividing by zero
  • Attempts to use a type conversion function such as int on a value that can’t be converted to an int
The following program contains various runtime errors. Can you spot any of them? After locating the error, run the program to see the error message. Can you correct the errors?
Notice the following important differences between syntax errors and runtime errors that can help you as you try to diagnose and repair the problem:
  • If the error message mentions SyntaxError, you know that the problem has to do with syntax: the structure of the code, the punctuation, etc.
  • If the program runs partway and then crashes, you know the problem is a runtime error. Programs with syntax errors don’t execute even one line.
Note that the error message for a runtime error will identify the line number where the error occurred, but the root cause of the error may be elsewhere. For example, the program above tries to multiply a user-entered numerical value by 0.08, but does not first convert the user input from string to float. An error will occur on line 2 where the multiplication is attempted, but the real problem is not the multiplication - it’s the missing type conversion.
Stay tuned for more details on the various types of runtime errors. We have a whole section of this chapter dedicated to that topic.
Check your understanding

Checkpoint 3.4.1.

    Which of the following is a run-time error?
  • Attempting to divide by 0.
  • Python cannot reliably tell if you are trying to divide by 0 until it is executing your program (e.g., you might be asking the user for a value and then dividing by that value—you cannot know what value the user will enter before you run the program).
  • Forgetting the closing parenthesis ) on a print statement.
  • This is a problem with the formal structure of the program. Python knows that each opening parenthesis must have a corresponding closing parenthesis, and can detect when one is missing simply by looking at the code without running it.
  • Forgetting to divide by 100 when printing a percentage amount.
  • This will produce the wrong answer, but Python will not consider it an error at all. The programmer is the one who understands that the answer produced is wrong.

Checkpoint 3.4.2.

    Who or what typically finds runtime errors?
  • The programmer.
  • Programmers rarely find all the runtime errors, there is a computer program that will do it for us.
  • The interpreter.
  • If an instruction is illegal to perform at that point in the execution, the interpreter will stop with a message describing the exception.
  • The computer.
  • Well, sort of. But it is a special thing in the computer that does it. The stand alone computer without this additional piece can not do it.
  • The teacher / instructor.
  • Your teacher and instructor may be able to find most of your runtime errors, but only because they have experience looking at code and possibly writing code. With experience runtime errors are easier to find. But we also have an automated way of finding these types of errors.
You have attempted of activities on this page.