我可以获取抛出异常的Python函数的局部变量吗?

时间:2012-01-30 04:28:01

标签: python exception-handling

我正在为项目编写自定义日志记录系统。如果函数抛出异常,我想记录它的局部变量。是否可以从捕获异常的except块访问raise函数的局部变量?例如:

def myfunction():
    v1 = get_a_value()
    raise Exception()

try:
    myfunction()
except:
    # can I access v1 from here?

6 个答案:

答案 0 :(得分:27)

如果你知道你的异常处理代码需要它,那么将值传递给异常通常是更简洁的设计。但是,如果您正在编写调试器或类似的东西,您将需要访问变量而不知道它们是哪些变量,您可以访问异常上下文中的任意变量抛出:

def myfunction():
    v1 = get_a_value()
    raise Exception()

try:
    myfunction()
except:
    # can I access v1 from here?
    v1 = inspect.trace()[-1][0].f_locals['v1']

trace函数描述了traceback函数的功能及其处理的{{1}}对象的格式。

答案 1 :(得分:2)

def myFunction()
    v1 = get_a_value()
    raise Exception(v1)


try:
    myFunction()
except Exception, e:
    v1 = e.args[0]

答案 2 :(得分:2)

您可以在框架对象中查找局部变量,您可以从sys.exc_info获取该局部变量。

>>> import sys
>>> def f(a):
...     b = a - 1
...     print 1.0 / b
...
>>> try:
...     f(1)
... except Exception, e:
...     print sys.exc_info()[2].tb_next.tb_frame.f_locals
...
{'a': 1, 'b': 0}

您必须包含适当数量的tb_next,具体取决于引发异常的堆栈深度。

答案 3 :(得分:1)

是的,如果它是您自己的异常类型。像这样:

>>> class MyExn(Exception):
...     def __init__(self, val):
...             self.val = val
...     def __str__(self):
...             print "something wrong with:", str(self.val)
... 
>>> def foo():
...     val = 42
...     raise MyExn(val)
... 
>>> foo()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in foo
__main__.MyExnsomething wrong with: 42

>>> # Or in a try block:
>>> try:
...     foo()
... except MyExn as e:
...     print e.val
... 
42
>>> 

答案 4 :(得分:0)

try:
    myfunction()
except:
    import sys
    type, value, tb = sys.exc_info()
    while tb.tb_next:
        tb = tb.tb_next
    frame = tb.tb_frame
    print frame.f_locals['v1']

答案 5 :(得分:0)

例如,在不使用raise Exception(some_def_var)且不使用“检查”之类的库的情况下,可用于“处理”异常。

但是,我认为如果要“实际”处理异常,最好使用raise Exception(some_def_var)之类的东西。 See @capfredf answer

class MyClass():

    def __init__(self):
        self.my_attr_I = ""
        self.my_attr_II = ""

    def my_def_I(self):
        try:
            self.my_def_II()
        except Exception as e:
            print(self.my_attr_I)
            print(self.my_attr_II)

    def my_def_II(self):
        self.my_attr_I = "TO EXCEPT I"
        self.my_attr_II = "TO except II"

        # NOTE: We will treat it as a general exception! By Questor
        zero_division = 1/0