Suppose there is such a piece of code:
try:
a = 1
b = 0
c = a / b
except Exception as e:
print(e)
Now I want to get the value of each variable before the exception occurs when the exception occurs, that is, get the result like a=1, b=0.
inspect.currentframe
This python should not be able to actively implement it, because if there is such a method, an exception occurs during multi-layer calls, and the corresponding data is recorded layer by layer and then returned, then this is likely to cause memory problems; And before an exception occurs, the virtual machine doesn't know that you have a problem. It's like your division-by-zero exception above is running a/b => 1/0, implemented in the i_pmod function code of PyIntobject , it is judged that the divisor is 0, an exception is triggered directly, and then the stack returns layer by layer to tell the user that an exception has occurred. There is no code related to the value of the symbol recorded in the try_block, so at most people make more detailed code in except. , humanized output
Use ipython to open pdb, and you can report which line has an error!
pdb
Add the following code:
Execute as follows: