Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Large try except block in Python - how to understand where was an exception?

I have a program (not mine) that has a large try - except block. Somewhere in this block there is an exception. what is the best way to find out the exact string of code where it happens?

like image 674
beSpark Avatar asked Feb 15 '23 10:02

beSpark


1 Answers

You can use print_exc in the except block

import traceback
traceback.print_exc()

Example:

import traceback
try:
    pass
    pass
    pass
    pass
    pass
    raise NameError("I dont like your name")
    pass
    pass
    pass
    pass
    pass
except Exception, e:
    traceback.print_exc()

Output

Traceback (most recent call last):
  File "/home/thefourtheye/Desktop/Test.py", line 8, in <module>
    raise NameError("I dont like your name")
NameError: I dont like your name
like image 128
thefourtheye Avatar answered Feb 17 '23 03:02

thefourtheye