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.
def area(self): raise NotImplementedError("subclasses must implement area()")
Try it
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.
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
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
RuntimeErrorfor "dictionary changed size during iteration", "Set changed size during iteration", "generator raised StopIteration", reusing a finished coroutine, and callingasyncio.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
RuntimeErrorfor a state problem that is not the caller's bad argument: "connection is closed", "start() called twice". For bad arguments useValueErrororTypeError; for domain errors make a custom exception. NotImplementedErroris notNotImplemented.NotImplementedis a value returned by binary special methods (__eq__,__add__) to tell Python "try the other operand".NotImplementedErroris an exception. Mixing them up is a classic bug:raise NotImplementedfails with aTypeError.- Abstract methods. With
abc.ABCand@abstractmethod, Python refuses to create the instance at all (TypeError: Can't instantiate abstract class), which catches the mistake earlier thanNotImplementedErrorat call time. - Placeholders. Use
raise NotImplementedErrorin stubs while developing, andpassor...only when doing nothing is the correct behavior. - Subclass if callers must catch it. A bare
RuntimeErroris hard to catch precisely, so defineclass ConnectionClosed(RuntimeError)for a condition callers handle. - Message wording for the built-in cases changes between versions, so never compare messages.