💥 Runtime Error

6th Grade 💻 Coding

A runtime error is a problem that only shows up while the program is running: the code is written correctly, but at some moment it’s asked to do something impossible, and the program stops.

Free · US Letter · no sign-up
Google Classroom Save to Pinterest Email me

Examples in Python: ZeroDivisionError — trying to divide by zero. NameError — using a name the computer doesn’t know, like a variable that hasn’t been created yet or a misspelled name. IndexError — asking for an item that doesn’t exist in a list, like item number 10 in a list of three.

Errors like these often depend on the input: the program works great with ordinary numbers and crashes when someone types 0 or a word. That’s why it’s important to test edge cases and add checks to your code before risky actions — for example, “if the number you’re dividing by is 0, show a message instead of dividing.”

For example

  • print(10 / 0) in Python stops the program with a ZeroDivisionError.
  • Print("x") with a capital P causes a NameError, because there’s no such name as Print.
  • In a list of three names, names[3] causes an IndexError — because the only indexes are 0, 1 and 2.

📌 Remember: Runtime = the code is fine, but halfway through it’s asked to do the impossible.

Quick quiz

  1. When does a runtime error show up?

  2. Which error does dividing by zero cause in Python?

  3. Why does Print("x") cause an error?

🔎 Ask Thinkapedia another question