I want to create a single line line from Exception that tells me what happened where (full backtracking is not required). The following information would be nice:
- file_name / linenumber
- type of exception
- exception description (what do you get from
str(e) ) - nice to have: function / method / class
I am currently doing the following:
import os ... try: os.nonexisting() except Exception as e: t = e.__traceback__ tbf = e.__traceback__.tb_frame print('%s:%d: %s in %s(): "%s" ' % os.path.basename(tbf.f_code.co_filename), t.tb_lineno, e.__class__.__name__, tbf.f_code.co_name, e))
which gives me:
foo.py:203: AttributeError in foo(): "'module' object has no attribute 'nonexisting'"
Is there a more elegant way to print the details in this example? I think about with. as
print(e.format('%f: %l: %t %F: "%w"'))
I would like to avoid importing additional modules except one for this purpose.
source share