Python尝试除了最后

发布于 2024-10-07 22:00:37 字数 759 浏览 6 评论 0原文

看来我还不太掌握异常处理的窍门。我不知所措:( 以下代码有时会返回此错误:

File "applications/pingback/modules/plugin_h_pingback.py", line 190, in ping
    db(table.id==id_).update(status=status)
UnboundLocalError: local variable 'status' referenced before assignment

我希望 status 始终已被分配一个值。难道是抛出了其他异常(可能是在内部的 try 中)并且finally 掩盖了它?

...
try:
    server_url = self._get_pingback_server(target)
except PingbackClientError, e:
    status = e.message
else:
    try:
        server = xmlrpclib.ServerProxy(server_url)
        status = server.pingback.ping(self.source, target)
    except xmlrpclib.Fault, e:
        status = e
finally:
    db(table.id==id_).update(status=status) # <-- UnboundLocalError
...

谢谢,HC

It looks like I don't quite have the hang of Exception handling yet. I'm at a loss :(
The following code sometimes returns this error:

File "applications/pingback/modules/plugin_h_pingback.py", line 190, in ping
    db(table.id==id_).update(status=status)
UnboundLocalError: local variable 'status' referenced before assignment

I would expect status to always have been assigned a value. Could it be that some other exception is thrown (perhaps in the inner try) and the finally obscures it?

...
try:
    server_url = self._get_pingback_server(target)
except PingbackClientError, e:
    status = e.message
else:
    try:
        server = xmlrpclib.ServerProxy(server_url)
        status = server.pingback.ping(self.source, target)
    except xmlrpclib.Fault, e:
        status = e
finally:
    db(table.id==id_).update(status=status) # <-- UnboundLocalError
...

Thanks, HC

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别念他 2024-10-14 22:00:37

您的代码并不总是为状态分配某些内容。我可以看到几种可能无法分配状态的方法,我在下面突出显示了它们:

try:
    server_url = self._get_pingback_server(target)
except PingbackClientError, e:
    # If evaluating `e.message` raises an exception then status is not set.
    status = e.message  # <--- here
else:
    try:
        # If either of these two lines fails with something other than
        # xmlrcplib.Fault, then status is not set.
        server = xmlrpclib.ServerProxy(server_url)             # <--- here
        status = server.pingback.ping(self.source, target)     # <--- here
    # If xmlrpclib.Fault is not defined status is not set.
    except xmlrpclib.Fault, e:                                 # <--- here
        status = e
finally:
    db(table.id==id_).update(status=status)

我怀疑最有可能出现错误的位置是在内部 try 块中,您只能在其中捕获 xmlrpclib.Fault 而不是其他类型的异常。

Your code doesn't always assign something to status. I can see a few ways that status might not be assigned and I've highlighted them below:

try:
    server_url = self._get_pingback_server(target)
except PingbackClientError, e:
    # If evaluating `e.message` raises an exception then status is not set.
    status = e.message  # <--- here
else:
    try:
        # If either of these two lines fails with something other than
        # xmlrcplib.Fault, then status is not set.
        server = xmlrpclib.ServerProxy(server_url)             # <--- here
        status = server.pingback.ping(self.source, target)     # <--- here
    # If xmlrpclib.Fault is not defined status is not set.
    except xmlrpclib.Fault, e:                                 # <--- here
        status = e
finally:
    db(table.id==id_).update(status=status)

I suspect that the most likely place for the error is in the inner try block where you are only catching xmlrpclib.Fault and not other types of exceptions.

热风软妹 2024-10-14 22:00:37

作为一个简单的解决方案,我会在任何块之外初始化状态:

status = None
try: 
    # etc

然后状态将始终被绑定。这不会解决任何未处理异常的问题,但会解决 UnboundLocalError。

(此外,在第一个块中,您使用 e.message 分配状态,在接下来的块中,您只需使用完整的错误 e,而不仅仅是消息。)

As a simple solution, I'd initialize status outside any blocks:

status = None
try: 
    # etc

Then status will always be bound. That will not solve the problem of any unhandled exception, but it will solve the UnboundLocalError.

(Also, in the first block you assing status with e.message, in the following block, you just use the complete error e, not just the message.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文