Python 3.2+Exceptionsexception
ResourceWarning
built-in exceptions
A resource was not released (e.g. an unclosed file; hidden by default).
Syntax
raise ResourceWarning
The default warning filters silence it, so an unclosed file usually leaves no trace — run with python -X dev or -W default when you suspect leaking descriptors. It fires whenever the garbage collector finally reaches the object, so the reported location rarely matches where the file was opened. The fix is a with block, never an except.
Examples
try:
raise ResourceWarning
except ResourceWarning as e:
print(type(e).__name__) # → ResourceWarning
See also
Open in the full glossarydocs.python.orgReport an inaccuracy