【发布时间】:2013-11-07 11:29:22
【问题描述】:
我正在尝试在 python3 / pygobject 中使用我的自定义 gobject 类之一中的属性进行一些异常处理。我的代码是这样的
try:
label = foo.label # This is a GObject.Property
except Exception:
label = "fallback"
我注意到解释器在尝试找出问题后从未找到 except 块,我想出了这个测试用例
from gi.repository import Gtk, GObject
class foo(GObject.Object):
@GObject.Property
def bar(self):
raise NotImplementedError
fish = foo()
print("Bar: ", fish.bar)
输出
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/gi/_gobject/propertyhelper.py", line 403, in obj_get_property
return prop.fget(self)
File "test.py", line 6, in bar
raise NotImplementedError
NotImplementedError
Bar: None
如您所见,即使出现异常,该属性也会返回 None 并且程序会继续运行。
我也不明白。
有人知道解决方法或解决方案吗?
【问题讨论】:
标签: python exception python-3.x pygobject gobject