Skip to content
Tutorials Center
My learning

Topics

Python Reference ยท Exceptions

Python RuntimeError and NotImplementedError

Reference for Python RuntimeError and NotImplementedError: when they are raised, changed-size-during-iteration, abstract methods, and NotImplemented vs the error.

RuntimeError is the general-purpose exception for an error that does not fit any other category. Python itself raises it in a few situations, such as changing a dictionary while looping over it or a generator that ends the wrong way. It is also the base class of RecursionError and NotImplementedError.

NotImplementedError marks code that is intentionally unfinished: a method in a base class that subclasses must override, or a feature that is planned but not written. Raise it yourself; Python only raises it for a few abstract cases inside the standard library.

Python
def area(self):
    raise NotImplementedError("subclasses must implement area()")

Try it

Python Python's own RuntimeErrors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python NotImplementedError in a base class

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Syntax

Python
raise RuntimeError("what went wrong")
raise NotImplementedError("not written yet")
raise NotImplementedError          # message is optional
Name Description
args The message and any other values passed when raising.

Hierarchy: Exception โ†’ RuntimeError โ†’ NotImplementedError, RecursionError, PythonFinalizationError (3.13+).

Usage notes

  • Python raises RuntimeError for "dictionary changed size during iteration", "Set changed size during iteration", "generator raised StopIteration", reusing a finished coroutine, and calling asyncio.run() from a running event loop. Fix the cause; do not catch it.
  • Looping and mutating. Loop over a copy (list(d), d.copy()) or build a new collection. Changing the value of an existing key is allowed; adding or removing keys is not. Lists do not raise, but skip items silently.
  • Raise your own RuntimeError for a state problem that is not the caller's bad argument: "connection is closed", "start() called twice". For bad arguments use ValueError or TypeError; for domain errors make a custom exception.
  • NotImplementedError is not NotImplemented. NotImplemented is a value returned by binary special methods (__eq__, __add__) to tell Python "try the other operand". NotImplementedError is an exception. Mixing them up is a classic bug: raise NotImplemented fails with a TypeError.
  • Abstract methods. With abc.ABC and @abstractmethod, Python refuses to create the instance at all (TypeError: Can't instantiate abstract class), which catches the mistake earlier than NotImplementedError at call time.
  • Placeholders. Use raise NotImplementedError in stubs while developing, and pass or ... only when doing nothing is the correct behavior.
  • Subclass if callers must catch it. A bare RuntimeError is hard to catch precisely, so define class ConnectionClosed(RuntimeError) for a condition callers handle.
  • Message wording for the built-in cases changes between versions, so never compare messages.