Python 获取上次引发的异常的引用
在python和/或ipython交互式解释器中,如何在最后一个未处理的异常上绑定名称?即相当于Python 获取上次引发的异常的引用,python,exception,exception-handling,stack-trace,callstack,Python,Exception,Exception Handling,Stack Trace,Callstack,在python和/或ipython交互式解释器中,如何在最后一个未处理的异常上绑定名称?即相当于 >>> try: ... 1/0 ... except Exception as potato: ... pass ... >>> format(potato) 'integer division or modulo by zero' 一定有点像 >>> 1/0 Traceback (most recent call last
>>> try:
... 1/0
... except Exception as potato:
... pass
...
>>> format(potato)
'integer division or modulo by zero'
一定有点像
>>> 1/0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero
>>> import sys
>>> potato = ???
>1/0
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
ZeroDivisionError:整数除法或模零除法
>>>导入系统
>>>马铃薯=???
当引发未处理的异常时,可以使用--pdb
开关调用ipython
,将您转储到python调试器(pdb)中
$ ipython --pdb
In [1]: 1/0
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
<ipython-input-2-05c9758a9c21> in <module>()
----> 1 1/0
ZeroDivisionError: integer division or modulo by zero
> /usr/lib/python2.7/bdb.py(177)_set_stopinfo()
176 def _set_stopinfo(self, stopframe, returnframe, stoplineno=0):
--> 177 self.stopframe = stopframe
178 self.returnframe = returnframe
ipdb> whatis
*** SyntaxError: SyntaxError('unexpected EOF while parsing', ('<string>', 0, 0, ''))
ipdb> where
/usr/lib/python2.7/bdb.py(43)reset()
41 linecache.checkcache()
42 self.botframe = None
---> 43 self._set_stopinfo(None, None)
44
45 def trace_dispatch(self, frame, event, arg):
> /usr/lib/python2.7/bdb.py(177)_set_stopinfo()
175
176 def _set_stopinfo(self, stopframe, returnframe, stoplineno=0):
--> 177 self.stopframe = stopframe
178 self.returnframe = returnframe
179 self.quitting = 0
$ipython--pdb
在[1]中:1/0
---------------------------------------------------------------------------
ZeroDivisionError回溯(最近一次呼叫上次)
在()
----> 1 1/0
ZeroDivisionError:整数除法或模零除法
>/usr/lib/python2.7/bdb.py(177)\u set\u stopinfo()
176定义设置停止信息(self、stopframe、returnframe、stoplineno=0):
-->177 self.stopframe=停止帧
178 self.returnframe=returnframe
ipdb>什么
***SyntaxError:SyntaxError('解析时出现意外的EOF',('',0,0',))
ipdb>在哪里
/usr/lib/python2.7/bdb.py(43)reset()
41 linecache.checkcache()
42 self.botframe=无
--->43自设置停止信息(无,无)
44
45 def trace_分派(自身、帧、事件、参数):
>/usr/lib/python2.7/bdb.py(177)\u set\u stopinfo()
175
176定义设置停止信息(self、stopframe、returnframe、stoplineno=0):
-->177 self.stopframe=停止帧
178 self.returnframe=returnframe
179自我退出=0
pdb
的文档在这里:您可以使用它:
>>> 1/0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero
>>> sys.last_value
ZeroDivisionError('integer division or modulo by zero',)
>>> type(sys.last_value)
<type 'exceptions.ZeroDivisionError'>
>1/0
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
ZeroDivisionError:整数除法或模零除法
>>>sys.last_值
ZeroDivisionError('整数除法或零模',)
>>>类型(系统最后值)
有趣的问题。。。