Python Glossary Русский

all Python 3Exceptionsexception

Exception

built-in exceptions

The base class of all ordinary (non-system-exit) exceptions; `except Exception` is the usual broad catch.

Syntax

raise Exception

except Exception swallows genuine data errors and your own typos alike: NameError, AttributeError and TypeError are all subclasses, so a broad catch turns a bug into a quietly printed message. Name the concrete class instead (ValueError, KeyError, ZeroDivisionError) and reserve Exception for the outermost layer where you log and exit. Unlike a bare except:, it leaves SystemExit, KeyboardInterrupt and GeneratorExit alone — they descend from BaseException, so Ctrl+C still stops the program.

Examples

try:
    raise Exception
except Exception as e:
    print(type(e).__name__)   # → Exception

See also

Open in the full glossarydocs.python.orgReport an inaccuracy