Skip to content

Informatics Practices · Ch 3 — Brief Overview of Python

Runtime Error

3.9.3

Runtime Error

A runtime error occupies the middle ground between a syntax error and a logical one: the statement is perfectly correct syntactically, so the program starts executing — but at some point the interpreter encounters a statement it simply cannot carry out, and the program terminates abnormally while it is running. That abnormal, mid-execution stop is the signature of a runtime error.

The classic example is division. Suppose a program contains a statement that performs a division operation. As a piece of Python, the statement is flawless, and the interpreter happily begins executing the program. But if, by mistake, the value of the denominator turns out to be zero at the moment the division is attempted, the operation is impossible — and the interpreter raises a runtime error such as "division by zero" and aborts the program.

Notice what distinguishes the three error types now that all have been seen:

  • a syntax error is caught before execution ever begins — the program does not run;
  • a logical error never interrupts the program — it silently yields wrong output;
  • a runtime error lets the program start, then kills it partway through. …