如何在记录错误的同时向用户显示错误?

发布于 2024-07-16 10:47:42 字数 496 浏览 8 评论 0原文

我正在使用 PyQt4 用户界面。 我已将 stderr 重定向到日志文件,以便于调试和故障排除,但现在我需要在发生错误时向用户显示错误消息。

我的问题是,我需要在发生异常时捕获异常并让用户知道它发生了,但仍然让回溯传播到 stderr (即日志文件)。

如果我这样做:

def updateResults(self):
    try:
        #code that updates the results
    except:
        #display error message box

这将捕获异常并且不会传播到错误日志。

有什么方法可以向用户显示消息,然后继续传播错误吗?

这行得通吗?

except, e:
    #display error message box
    raise e

有更好的方法来实现我的目标吗?

I'm using a PyQt4 user interface. I've redirected stderr to a log file for easy debugging and trouble-shooting, but now I need to display error messages to the user when an error occurs.

My issue is that I need to catch an exception when it happens and let the user know that it happened, but still let the traceback propagate to stderr (i.e. the log file).

If I do something like this:

def updateResults(self):
    try:
        #code that updates the results
    except:
        #display error message box

This will catch the exception and not propogate to the error log.

Is there some way to show the user the message and then continue to propogate the error?

Would this work?

except, e:
    #display error message box
    raise e

Is there a better way to accomplish my goal?

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

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

发布评论

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

评论(3

慵挽 2024-07-23 10:47:42

我认为你以错误的方式思考这个问题。 您不应该仅仅为了进一步记录它而重新引发错误。 在 Python 中执行此操作的规范方法是使用日志记录模块。 改编自文档:

import logging
LOG_FILENAME = '/tmp/logging_example.out'
logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG,)

...

try:
    # code
except:
    logging.debug('Something bad happened', exc_info=True)
    # display message box
    # raise (if necessary)

这提供了比依赖 sys.stdout 上产生的错误更加灵活的日志记录系统。 如果可以通过某种方式从异常中恢复,则可能不需要重新引发异常。

I think you are thinking about this in the wrong way. You shouldn't be re-raising the error simply to log it further down the line. The cannonical way of doing this in Python is to use the logging module. Adapted from the docs:

import logging
LOG_FILENAME = '/tmp/logging_example.out'
logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG,)

...

try:
    # code
except:
    logging.debug('Something bad happened', exc_info=True)
    # display message box
    # raise (if necessary)

This gives a far more flexible logging system than relying on errors produced on sys.stdout. You may not need to re-raise the exception if you can recover from the exception in some way.

梦过后 2024-07-23 10:47:42

确实如此,但是您可以重新

raise

引发当前处理的异常。

Exactly, but you can just

raise

which will re-raise the currently handled exception.

御守 2024-07-23 10:47:42

一些附加信息:(

使用 PyQt4)您还需要将 sys.excepthook 重新绑定到您自己的函数以捕获所有未捕获异常。 否则 PyQt 只会将它们打印到控制台,这可能不是您需要的......

import sys

def excepthook(exc_type, exc_val, tracebackobj):
    # do something useful with the uncaught exception
    ...

def main():
    # rebind excepthook
    sys.excepthook = excepthook
    ...

Some additional information:

(With PyQt4) you will also need to rebind sys.excepthook to your own function to catch all uncaught exceptions. Otherwise PyQt will just print them to the console, which may not be what you need...

import sys

def excepthook(exc_type, exc_val, tracebackobj):
    # do something useful with the uncaught exception
    ...

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