我是靠谱客的博主 干净朋友,最近开发中收集的这篇文章主要介绍python捕获不到异常,在Python中记录未捕获的异常,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

How do you cause uncaught exceptions to output via the logging module rather than to stderr?

I realize the best way to do this would be:

try:

raise Exception, 'Throwing a boring exception'

except Exception, e:

logging.exception(e)

But my situation is such that it would be really nice if logging.exception(...) were invoked automatically whenever an exception isn't caught.

解决方案

As Ned pointed out, sys.excepthook is invoked every time an exception is raised and uncaught. The practical implication of this is that in your code you can override the default behavior of sys.excepthook to do whatever you want (including using logging.exception).

As a straw man example:

>>> import sys

>>> def foo(exctype, value, tb):

... print 'My Error Information'

... print 'Type:', exctype

... print 'Value:', value

... print 'Traceback:', tb

...

Override sys.excepthook:

>>> sys.excepthook = foo

Commit obvious syntax error (leave out the colon) and get back custom error information:

>>> def bar(a, b)

My Error Information

Type:

Value: invalid syntax (, line 1)

Traceback: None

最后

以上就是干净朋友为你收集整理的python捕获不到异常,在Python中记录未捕获的异常的全部内容,希望文章能够帮你解决python捕获不到异常,在Python中记录未捕获的异常所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部