我试图在 python 2.7.x 中引发异常,其中包含消息中的 unicode。我似乎无法让它工作。
是否不支持或不建议在错误消息中包含 unicode?还是我需要查看 sys.stderr?
# -*- coding: utf-8 -*-
class MyException(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return self.value
def __repr__(self):
return self.value
def __unicode__(self):
return self.value
desc = u'something bad with field \u4443'
try:
raise MyException(desc)
except MyException as e:
print(u'Inside try block : ' + unicode(e))
# here is what i wish to make work
raise MyException(desc)
运行脚本会产生以下输出。在我的 try/except 中,我可以毫无问题地打印字符串。
我的问题不在尝试/除外。
Inside try block : something bad with field 䑃
Traceback (most recent call last):
File "C:\Python27\lib\bdb.py", line 387, in run
exec cmd in globals, locals
File "C:\Users\ghis3080\r.py", line 25, in <module>
raise MyException(desc)
MyException: something bad with field \u4443
提前致谢。