Este pequeno código que deixo aqui, ajuda a resolver isso, apresentando 2 pequenos detalhes de 2 módulos do Python, o sys e o traceback.
1: import sys, traceback
2:
3: if __name__ == '__main__':
4: a = {}
5:
6: print("Exception print Without Traceback")
7: try:
8: print(a[0])
9: except Exception as ex:
10: print(ex)
11:
12: print("Exception print With Traceback")
13: try:
14: print(a[0])
15: except Exception:
16: (exType, val, tb) = sys.exc_info()
17: traceback.print_exception(exType, val, tb)
18:
O resultado é este.
[claymore@ArchLinux32 Tasklets]$ python3 tracebackTests.py
Exception print Without Traceback
0
Exception print With Traceback
Traceback (most recent call last):
File "tracebackTests.py", line 21, in
print(a[0])
KeyError: 0
Documentação:
sys.exc_info()
traceback.print_exception(type, value, traceback, limit=None, file=None, chain=True)
Fiquem bem!