【发布时间】:2012-10-05 13:54:49
【问题描述】:
以下来自django源码(Django-1.41/django/utils/encoding.py);
try:
s = unicode(str(s), encoding, errors)
except UnicodeEncodeError:
if not isinstance(s, Exception):
raise
# If we get to here, the caller has passed in an Exception
# subclass populated with non-ASCII data without special
# handling to display as a string. We need to handle this
# without raising a further exception. We do an
# approximation to what the Exception's standard str()
# output should be.
s = u' '.join([force_unicode(arg, encoding, strings_only,
errors) for arg in s])
我的问题是:在什么情况下s 会是 Exception 的一个实例?
当 s 是 Exception 的一个实例,并且 s 既没有 str 也没有 repr 属性。比这种情况发生。是这样吗?
【问题讨论】:
-
我可以用 Python 写
raise "a_string"吗? -
raise 的唯一参数表示要引发的异常。这必须是异常实例或异常类(从 Exception 派生的类)。