Issue 14655: traceback module docs should show how to print/fomat an exception object - Python tracker

This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Expand traceback module API to accept just an exception as an argument
View: 26389
Assigned To: docs@python Nosy List: Arfrever, amaury.forgeotdarc, docs@python, iritkatriel, r.david.murray
Priority: normal Keywords:

Created on 2012-04-23 21:22 by r.david.murray, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (5)
msg159084 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-04-23 21:22
Suppose you have an exception object acquired from somewhere.  The exception is no longer active on the stack.  Now you want to format the exception like format_exception would (to log it, perhaps).  To do this you apparently need to call:

  format_exception(type(exc), exc, exc.__traceback__)

This seems redundant, so it would be nice to have a simpler call.  Too bad the name format_exception is already taken.  But, leaving that aside, the above is not documented in the traceback module.  The last example shows the (type(exc), exc) call form for format_exception_only, but that's as close as it gets.
msg159091 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2012-04-23 22:08
Why not reuse format_exception(exc)?
in 2.7 you could write "raise type, value, traceback", or "raise value".
I suggest that traceback.format_exception take the same route, and accept both (type, value, traceback) and (value).
msg159102 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-04-23 23:36
Sounds OK to me, though I am worried others will think that kind of variable signature (where the type of the first argument depends on the number of arguments passed) is bad.
msg380338 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-11-04 16:23
As of 3.5 there is this option:

traceback.TracebackException.from_exception(exc).format()


Perhaps that can be mentioned in the doc for traceback.format_exception().
msg384659 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-01-08 13:12
In issue 26389 the api was changed so that now format_exception(exc) works.