Menu
Coddy logo textTech

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.

By Kevin Spektor, Co-founder & CTO

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

  1. The program reaches an operation, here sum(scores) / len(scores).
  2. 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.
  3. 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.
  4. 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.
  5. 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.

ErrorExampleMessage
ZeroDivisionError10 / 0division by zero
IndexError["red", "green", "blue"][3]list index out of range
KeyError{"theme": "dark"}["font"]'font'
ValueErrorint("twenty")invalid literal for int() with base 10: 'twenty'
TypeError"Total: " + 5can only concatenate str (not "int") to str
AttributeErrorcalling .upper() on None'NoneType' object has no attribute 'upper'
FileNotFoundErroropen("missing.txt")[Errno 2] No such file or directory: 'missing.txt'
RecursionErrora function that calls itself forevermaximum 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

  1. Reproduce it. Run the program again with the same input and confirm you get the same error.
  2. Read the last line of the traceback, then find the matching line in your own code.
  3. Look at the values involved. Print them just before the failing line, or stop there with a debugger breakpoint.
  4. 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?
A syntax error is found before the program starts, because the code breaks the grammar of the language, so no line runs. A runtime error happens later, while the program is running, when a valid line meets a value it cannot handle. Every line before it has already run and may have printed output.
Why do I keep getting runtime errors?
Usually because the code assumes something about its data that is not always true: that a list is never empty, that a file exists, that the user types a number. Each assumption works for the inputs you tested and fails for one you did not. Test with empty, zero, missing and very large inputs, and check or handle those cases explicitly.
What does Runtime Error mean on LeetCode?
It means your solution compiled but crashed on at least one test case. The usual causes are an index out of range, dividing by zero, using a null or 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?
Not quite. In Python and Java, most runtime errors are reported as exceptions, which is what lets a program catch them. But languages such as C have runtime errors and no exceptions at all. In Java, RuntimeException is also the name of one specific family of exceptions, the unchecked ones such as NullPointerException.
Can a compiler catch runtime errors?
Only some of them. A compiler or a type checker such as mypy can prove that certain mistakes are impossible, for example adding text to a number. But it cannot know values that arrive while the program runs, such as a number typed by the user that turns out to be zero. Those errors can only be found by running the code or handled by checking the input.
Coddy programming languages illustration

Learn to code with Coddy

GET STARTED