导入 Python 异常
Importing Python Exceptions
在 foo.py 中给出以下代码:
class Error(Exception):
pass
class UnexpectedParameterType(Error):
pass
class Human(models.Manager):
def create_human(self, name):
if not isinstance(name, str):
raise UnexpectedParameterType
human = Human(name = name)
return human
我是否必须在 bar.py 中显式导入我的异常 class 以便捕获抛出的异常?像这样:
from foo import UnexpectedParameterType, Human
human = Human()
try:
human.create_human(123)
except UnexpectedParameterType:
return "Cannot create human."
我在这里暗示的是能够做这样的事情:
from foo import Human
[...]
except Human.UnexpectedParameterType:
return "Cannot create human."
非常感谢所有答案,谢谢!欢迎在 Python.
中分享您处理异常的个人最佳实践
您不能执行 Human.UnexpectedParameterType
,因为 Human
没有名为 UnexpectedParameterType
的属性。
这就是为什么您通常想要 import foo
而不是 from foo import ...
。如果你这样做的话,更容易追踪到什么东西去了。
您可以做的是创建一个基础 class,它有自己的例外。这可能类似于
# foo.py
class Human(object):
class Error(Exception):
pass
class UnexpectedParameterType(Error):
pass
def throw_bad_parm(self):
raise self.UnexpectedParameterType
# the rest of your function in Human
演示:
import foo
a = foo.Human()
try:
a.throw_bad_parm()
except foo.Human.UnexpectedParameterType as e:
print("You can't do that because of {!r}".format(e))
# You can't do that because of UnexpectedParameterType()
这与 @staticmethod
的工作方式大致相同——您将逻辑上属于您代码一部分的内容捆绑到其他 class 中,即使它们不依赖于那些 classes 操作。通过这种方式,您可以执行以下操作:
class HTTPHandler(object):
class Exception404(Exception): pass
class Exception403(Exception): pass
...
你不能做 Human.UnexpectedParameterType
,因为 UnexpectedParameterType
不是 Human
class 的属性;它是 foo
模块的一部分。你可以这样做
import foo
human = foo.Human()
try:
human.create_human(123)
except foo.UnexpectedParameterType:
return "Cannot create human."
否则,是的,您必须明确导入它。
然而,在这种特定情况下,Python 已经有一个表示错误类型的例外:TypeError
。因此,相反,您可以加注 TypeError
,然后再捕获 TypeError
。 (TypeError
是内置类型,因此您无需显式导入它。)
在 foo.py 中给出以下代码:
class Error(Exception):
pass
class UnexpectedParameterType(Error):
pass
class Human(models.Manager):
def create_human(self, name):
if not isinstance(name, str):
raise UnexpectedParameterType
human = Human(name = name)
return human
我是否必须在 bar.py 中显式导入我的异常 class 以便捕获抛出的异常?像这样:
from foo import UnexpectedParameterType, Human
human = Human()
try:
human.create_human(123)
except UnexpectedParameterType:
return "Cannot create human."
我在这里暗示的是能够做这样的事情:
from foo import Human
[...]
except Human.UnexpectedParameterType:
return "Cannot create human."
非常感谢所有答案,谢谢!欢迎在 Python.
中分享您处理异常的个人最佳实践您不能执行 Human.UnexpectedParameterType
,因为 Human
没有名为 UnexpectedParameterType
的属性。
这就是为什么您通常想要 import foo
而不是 from foo import ...
。如果你这样做的话,更容易追踪到什么东西去了。
您可以做的是创建一个基础 class,它有自己的例外。这可能类似于
# foo.py
class Human(object):
class Error(Exception):
pass
class UnexpectedParameterType(Error):
pass
def throw_bad_parm(self):
raise self.UnexpectedParameterType
# the rest of your function in Human
演示:
import foo
a = foo.Human()
try:
a.throw_bad_parm()
except foo.Human.UnexpectedParameterType as e:
print("You can't do that because of {!r}".format(e))
# You can't do that because of UnexpectedParameterType()
这与 @staticmethod
的工作方式大致相同——您将逻辑上属于您代码一部分的内容捆绑到其他 class 中,即使它们不依赖于那些 classes 操作。通过这种方式,您可以执行以下操作:
class HTTPHandler(object):
class Exception404(Exception): pass
class Exception403(Exception): pass
...
你不能做 Human.UnexpectedParameterType
,因为 UnexpectedParameterType
不是 Human
class 的属性;它是 foo
模块的一部分。你可以这样做
import foo
human = foo.Human()
try:
human.create_human(123)
except foo.UnexpectedParameterType:
return "Cannot create human."
否则,是的,您必须明确导入它。
然而,在这种特定情况下,Python 已经有一个表示错误类型的例外:TypeError
。因此,相反,您可以加注 TypeError
,然后再捕获 TypeError
。 (TypeError
是内置类型,因此您无需显式导入它。)