traceback.format_exception_only
has arguments (etype, value)
. If I have an exception e
that I want to format, in what situation would I not want to call format_exception_only(type(e), e)
?
sys.exc_info
returns a type, value, and traceback. The __exit__
method of a context manager has the same three things as parameters. Is the type not redundant? Can it be equal to something other than type(value)
?
Historical reasons.
Way back in the 1.x days, this information wasn't all bundled up in a single object. The exception type was a string(!) rather than a class, the value was some arbitrary object, and the traceback wasn't attached to either of those objects. All the type, value, traceback
APIs were built around this design, where these objects had to be passed around separately:
MyException = "MyException"
raise MyException, 3
Some time in the 1.x line (I think 1.2), they added class-based exceptions, and some time in the 2.x line (I think 2.6, since the 2.5 branch code path seems to only raise a warning), string exceptions were removed, but the old function signatures stuck around.