What Is a Runtime Error?
A runtime error is an error that happens while a program is running, when an operation cannot be carried out with the values it has, such as dividing by zero or reading past the end of a list. The program stops at that line unless the error is handled.
Updated September 24, 2026
A runtime error waits until the program is already running. The code passes every check before it starts, runs correctly for a while, and then reaches an operation that cannot be performed with the values it has at that moment. This function works for one list and fails for another:
85.0
Traceback (most recent call last):
File "main.py", line 5, in <module>
print(average([]))
^^^^^^^^^^^
File "main.py", line 2, in average
return sum(scores) / len(scores)
~~~~~~~~~~~~^~~~~~~~~~~~~
ZeroDivisionError: division by zero
The first call prints 85.0. The second call passes an empty list, so len(scores) is 0 and the division cannot happen. Nothing was wrong with the grammar, so this is not a syntax error. The problem only exists for certain data, which is why a runtime error can appear after a program has worked correctly a hundred times.
What happens when a runtime error occurs
- The program reaches an operation, here
sum(scores) / len(scores). - The runtime checks it before carrying it out. CPython tests the divisor before dividing, the JVM checks every array index, and Python checks that a dictionary key exists before returning its value.
- The check fails, so the runtime creates an error object. In Python and Java this object is an exception, and it records the type of error, a message, and where it happened.
- The runtime looks for code that handles that type of error, first in the current function, then in the function that called it, and so on outward.
- If nothing handles it, the program stops. Python prints a traceback and exits with status 1. Java prints a stack trace that starts with
Exception in thread "main".
Languages without these checks behave differently. C does not check array indexes or pointers, so a bad memory access either corrupts data silently or makes the operating system end the program with a segmentation fault.
Everything the program did before the failing line has already happened. This loop prints three colors before it fails on the fourth:
red
green
blue
Traceback (most recent call last):
File "main.py", line 4, in <module>
print(colors[i])
~~~~~~^^^
IndexError: list index out of range
How to read a traceback
Read a Python traceback from the bottom up. The last line names the error type and the message: ZeroDivisionError: division by zero. The lines above it list the chain of calls, oldest first and most recent last. In the first example, line 5 called average, and line 2, inside average, is where the division failed.
The line where the error happened is not always the line to change. Line 2 of average is correct. The real question is how an empty list reached it, so trace the bad value back to where it came from and fix it there. When a traceback runs through library files you did not write, look for the last line that points into your own file.
Common runtime errors
Each of these messages is copied from Python 3.12.
| Error | Example | Message |
|---|---|---|
ZeroDivisionError | 10 / 0 | division by zero |
IndexError | ["red", "green", "blue"][3] | list index out of range |
KeyError | {"theme": "dark"}["font"] | 'font' |
ValueError | int("twenty") | invalid literal for int() with base 10: 'twenty' |
TypeError | "Total: " + 5 | can only concatenate str (not "int") to str |
AttributeError | calling .upper() on None | 'NoneType' object has no attribute 'upper' |
FileNotFoundError | open("missing.txt") | [Errno 2] No such file or directory: 'missing.txt' |
RecursionError | a function that calls itself forever | maximum recursion depth exceeded |
Java has direct equivalents, such as ArithmeticException, ArrayIndexOutOfBoundsException, NumberFormatException and NullPointerException. An integer division by zero that nothing catches prints this in Java:
Exception in thread "main" java.lang.ArithmeticException: / by zero
JavaScript is more forgiving with numbers: 10 / 0 is Infinity, not an error. It fails when you use a value that is not there. Node.js prints this for user.name when user is undefined:
TypeError: Cannot read properties of undefined (reading 'name')
How to fix a runtime error
- Reproduce it. Run the program again with the same input and confirm you get the same error.
- Read the last line of the traceback, then find the matching line in your own code.
- Look at the values involved. Print them just before the failing line, or stop there with a debugger breakpoint.
- Fix the cause. Correct the index, guard the special case, or validate the input before using it.
For the average function, the fix is to decide what the average of no scores should be and say so in the code:
85.0
0
Some failures are outside your control: a file that was deleted, a network that times out, a user who types letters where a number belongs. You cannot prevent those by fixing code, so you catch them with exception handling and decide what the program does next. What you should not do is wrap a bug in try and hide it. An IndexError from a loop that counts one step too far is a mistake to fix, not an error to catch.
Runtime error messages on Windows and in Excel
The term also appears outside your own code. A Windows dialog titled Microsoft Visual C++ Runtime Library with the words Runtime Error! means a program written in C++ hit a fatal error and the C++ runtime ended it. As a user you cannot change that code, so the usual remedies are updating the program, repairing or reinstalling the Microsoft Visual C++ Redistributable packages, and, for games, updating the graphics driver.
Excel macros written in VBA report numbered runtime errors. Run-time error '9': Subscript out of range means the macro asked for a worksheet or array element that does not exist, and Run-time error '1004' is a general error raised when an operation on a workbook, sheet or range fails. The idea is the same as in Python: valid code met a value it could not handle.
Where to go next
Runtime errors have two siblings: the syntax error, found before the program starts, and the logic error, which never produces a message at all. To recover from runtime errors instead of crashing, read exception handling and then the Python guides to exceptions and debugging errors. The Python course has you write and fix code like this in every lesson.
Frequently Asked Questions
What is the difference between a runtime error and a syntax error?
Why do I keep getting runtime errors?
What does Runtime Error mean on LeetCode?
None value, or recursion so deep that the stack overflows. Run the failing input shown on the results page yourself, and check the edge cases: an empty array, one element, and the largest allowed size.Is a runtime error the same as an exception?
RuntimeException is also the name of one specific family of exceptions, the unchecked ones such as NullPointerException.