CertGrid CertGrid
Concepts·Certified Associate Python Programmer

Python Exception Objects and Arguments

PCAP treats an exception as an object rather than a label, and objective 2.1 expects you to know what is on it. `as e` binds an instance; `e.args` is the tuple the constructor was given; `str(e)` is derived from that tuple and behaves differently for one argument, several, and none. Some exceptions carry extra attributes - `OSError` has three - and the name `e` is deleted when the clause ends.

Exceptions in Depth Guide 2 of 25 Intermediate

Written against the versions above. The `del e` at the end of an `except ... as e` clause has been the behaviour since Python 3.0 and is one of the few Python 2 to 3 differences in exception handling. Nothing on this page has changed since.

One machine, and any shell with Python 3 will do - these exams test the language, not a distribution.
Server NameIP AddressOSRolesCPURAMHDD
RUNNER01192.168.0.27Ubuntu 26.04 LTSPython 3.14.4 - the only machine this path needs2 Core4 GB50 GB

Before you start

  1. as e binds an object, not a name

    except ValueError as e gives you the instance that was raised. It has a class, a string form, a repr and an args tuple, and you can ask it anything you can ask any object.

    bash Example session
    cat > ~/py/excobject.py <<'PY'try:    int("x")except ValueError as e:    print("type()", type(e).__name__)    print("str()  ", str(e))    print("repr() ", repr(e))    print("args   ", e.args)    print("is it an instance of Exception?", isinstance(e, Exception))PYpython3 ~/py/excobject.pytype() ValueErrorstr()   invalid literal for int() with base 10: 'x'repr()  ValueError("invalid literal for int() with base 10: 'x'")args    ("invalid literal for int() with base 10: 'x'",)is it an instance of Exception? True

    Expected resultValueError, the message, the repr with brackets, a one-item tuple, and True.

    Success conditionYou can inspect a caught exception rather than just reporting it.

  2. args holds whatever raise was given

    e.args is a tuple of exactly the arguments the exception was constructed with. str(e) is then derived from it, and the derivation has three cases.

    bash Example session
    cat > ~/py/excargs.py <<'PY'def show(*values):    try:        raise ValueError(*values)    except ValueError as e:        print("raise ValueError" + str(values), "-> args", repr(e.args),              "| str()", repr(str(e)))  show("one message")show("a", "b", 3)show()PYpython3 ~/py/excargs.pyraise ValueError('one message',) -> args ('one message',) | str() 'one message'raise ValueError('a', 'b', 3) -> args ('a', 'b', 3) | str() "('a', 'b', 3)"raise ValueError() -> args () | str() ''

    Expected resultOne argument gives that string; three give the tuple's repr; none gives an empty string.

    Success conditionYou can predict str(e) from e.args.

  3. KeyError is the exception to that rule, sort of

    {}["missing"] raises KeyError with the key as its single argument. Because the key is a string, str(e) shows it with quotes - which reads oddly in a log and confuses people into thinking the quotes are part of the key.

    bash Example session
    cat > ~/py/keyerrorstr.py <<'PY'try:    {}["missing"]except KeyError as e:    print("args ", e.args)    print("str  ", repr(str(e)))    print("the key itself:", e.args[0])PYpython3 ~/py/keyerrorstr.pyargs  ('missing',)str   "'missing'"the key itself: missing

    Expected resultargs ('missing',), str "'missing'", then the key without quotes.

    Success conditionYou can get the missing key out of a KeyError.

  4. Some exceptions carry more than a message

    OSError and its subclasses have named attributes: the C errno, the system's description, and the filename involved.

    bash Example session
    cat > ~/py/oserrorattrs.py <<'PY'try:    open("/no/such/file")except OSError as e:    print("class    ", type(e).__name__)    print("errno    ", e.errno)    print("strerror ", e.strerror)    print("filename ", e.filename)    print("str()    ", str(e))PYpython3 ~/py/oserrorattrs.pyclass     FileNotFoundErrorerrno     2strerror  No such file or directoryfilename  /no/such/filestr()     [Errno 2] No such file or directory: '/no/such/file'

    Expected resultFileNotFoundError, errno 2, No such file or directory, and the path.

    Success conditionYou can extract the useful parts of a file error.

  5. The name is deleted when the clause ends

    This surprises people who expect e to be an ordinary variable. At the end of an except ... as e block, Python deletes e.

    bash Example session
    cat > ~/py/excscope.py <<'PY'try:    1 / 0except ZeroDivisionError as e:    print("inside the clause:", e)print(e)PYpython3 ~/py/excscope.pyinside the clause: division by zeroTraceback (most recent call last):  File "/home/sysadmin/py/excscope.py", line 5, in <module>    print(e)          ^NameError: name 'e' is not defined[exit 1]

    Expected resultinside the clause: division by zero, then NameError: name 'e' is not defined.

    Success conditionYou know how long the exception name lasts.

  6. What the exam does with this

    This part of objective 2.1 produces object-shaped questions rather than control-flow ones:

    "What is e.args after raise ValueError('a', 'b')?" - ('a', 'b').

    "What does print(e) show for that same exception?" - ('a', 'b'), the tuple's repr.

    "How do you get the exception's class name?" - type(e).__name__.

    "How do you get the key from a KeyError?" - e.args[0].

    "Is e available after the handler?" - no.

    "What type is args?" - a tuple, always.

    guide 3 finishes the exceptions section.

    bash
    rm -f ~/py/excobject.py ~/py/excargs.py ~/py/keyerrorstr.py ~/py/oserrorattrs.py ~/py/excscope.py && ls -A ~/py

    Expected resultAn empty scratch directory.

    Success conditionYou can treat a caught exception as the object it is.

Troubleshooting

Official sources