不要在异常堆栈中显示Python提升行 [英] Don't show Python raise-line in the exception stack

查看:253
本文介绍了不要在异常堆栈中显示Python提升行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在我的Python库中引发我自己的异常时,异常堆栈将提升行本身显示为堆栈的最后一个项目。这显然不是一个错误,在概念上是正确的,但是将重点放在对外部使用代码进行调试时无用的东西,例如作为模块。



有没有办法避免这种情况,强制Python将最后一个堆栈项显示为最后一个堆栈项,就像标准的Python库一样。

解决方案

到期警告:修改解释器的行为通常是皱眉的。在任何情况下,查看错误的确切位置可能有助于调试,特别是如果某个功能可能会由于几个不同的原因引起错误。



如果您使用 traceback 模块,并替换 sys.excepthook 与自定义函数,可能是这样做的。但是,更改将影响整个程序的错误显示,而不仅仅是您的模块,因此可能不推荐使用。



您还可以看看将代码放在try / except块,然后修改错误并重新提高。但是,您的时间可能更好,导致意外错误不太可能,并为可能出现的错误信息撰写信息。


When I raise my owns exceptions in my Python libraries, the exception stack shows the raise-line itself as the last item of the stack. This is obviously not an error, is conceptually right, but points the focus on something that is not useful for debugging when you're are using code externally, for example as a module.

Is there a way to avoid this and force Python to show the previous-to-last stack item as the last one, like the standard Python libraries.

解决方案

Due warning: modifying the behaviour of the interpreter is generally frowned upon. And in any case, seeing exactly where an error was raised may be helpful in debugging, especially if a function can raise an error for several different reasons.

If you use the traceback module, and replace sys.excepthook with a custom function, it's probably possible to do this. But making the change will affect error display for the entire program, not just your module, so is probably not recommended.

You could also look at putting code in try/except blocks, then modifying the error and re-raising it. But your time is probably better spent making unexpected errors unlikely, and writing informative error messages for those that could arise.

这篇关于不要在异常堆栈中显示Python提升行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆