[英]Exception in Python Exception Class's __str__ Missed
Does anybody know how to catch exceptions in python Exception's derived classes's __str__
function? 有人知道如何在python Exception派生类的__str__
函数中捕获异常吗? And how to debug that __str__
using pdb since I find it doesn't work when invoke pdb.set_trace()
or setting breakpoint in pdb in function __str__
. 以及如何使用pdb调试__str__
因为我发现当调用pdb.set_trace()
或在函数__str__
中的pdb中设置断点时,它__str__
。
Example code: 示例代码:
class Ex(Exception):
def __str__(self):
raise KeyError('1')
raise Ex()
The output is 输出是
Traceback (most recent call last):
File "./exex.py", line 10, in <module>
raise Ex()
__main__.Ex
While I think the output should be something like 虽然我认为输出应该像
KeyError: '1'
And if I add pdb.set_trace()
before raise KeyError('1')
, python runtime does not interrupt at pdb.set_trace()
. 而且,如果我在raise KeyError('1')
pdb.set_trace()
raise KeyError('1')
pdb.set_trace()
之前添加pdb.set_trace()
,则python运行时不会在pdb.set_trace()
处中断。 So does anyone know how to debug Exception.__str__
when it is a little complex? 那么有人知道Exception.__str__
有点复杂时如何调试吗?
Sorry for the pdb.set_trace()
case. 很抱歉pdb.set_trace()
情况。 It works. 有用。 However, if you forget to put import pdb
, then python slightly ignore the exception which looks like pdb.set_trace()
is ignored. 但是,如果您忘记放置import pdb
,那么python会略微忽略看起来像pdb.set_trace()
的异常。
While setting breakpoint in pdb still does not work. 虽然在pdb中设置断点仍然不起作用。
You don't need to override __str__
to get a message. 您无需重写__str__
即可获得消息。 Just do: 做就是了:
class Ex(Exception):
pass
raise Ex('Key Error 1')
Prints: 印刷品:
Traceback (most recent call last):
File "Untitled.py", line 4, in <module>
raise Ex('Key Error 1')
__main__.Ex: Key Error 1
Python handles exceptions differently because it's in the process of printing an exception. Python处理异常的方式不同,因为它正在打印异常。 Acknowledging another exception at that point would only to confuse the programmer, so Python swallows it and does the best it can. 那时承认另一个异常只会使程序员感到困惑,因此Python会吞并它并尽其所能。
Write your classes' __str__
to always return a string and you'll be happier. 编写类的__str__
总是返回一个字符串,您会更快乐。
I had the same problem the other day. 前几天我遇到了同样的问题。 I ended up wrapping my exception's __str__
method in a try/except block. 我最终将我的异常的__str__
方法包装在try / except块中。 Something like this: 像这样:
import traceback
class ex(Exception):
def do_str(self):
return "ex: %s" % self.notexist
def check_str(self):
try:
return self.do_str()
except:
traceback.print_exc()
raise
__str__ = check_str
raise ex()
Which when you run it prints: 运行时会打印:
Traceback (most recent call last):
File "t100.py", line 16, in <module>
raise ex()
__main__.exTraceback (most recent call last):
File "t100.py", line 9, in check_str
return self.do_str()
File "t100.py", line 5, in do_str
return "ex: %s" % self.notexist
AttributeError: 'ex' object has no attribute 'notexist'
FWIW, there's a Python bug I opened some while ago which at least makes the output a little bit less confusing: issue22836 . FWIW,我前段时间打开了一个Python错误,至少使输出的混乱程度降低了: issue22836 。
There's also a patch, but unfortunately it's not merged so far. 也有一个补丁,但是很遗憾,到目前为止还没有合并。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.