Python,不带参数引发异常

发布于 2024-09-10 04:45:13 字数 688 浏览 2 评论 0原文

我想知道在没有参数的情况下引发异常的最佳实践。 在Python官方文档中,你可以看到这个:(

try:
    raise KeyboardInterrupt

http://docs.python.org/tutorial /errors.html 第 8.6 章)

,在一些不同的代码中,例如 Django 或 Google 代码,您可以看到以下内容:

  def AuthenticateAndRun(self, username, password, args):
    raise NotImplementedError()

(http://code.google.com/p/neatx/source/browse/trunk/neatx/lib/auth.py )

在没有参数的情况下,异常会在引发之前实例化。 实例化不带参数的异常的目的是什么?我什么时候应该使用第一种情况或第二种情况?

提前致谢 法比安

I would like to know the best practice about raising an exception without arguments.
In the official python documentation, you can see this :

try:
    raise KeyboardInterrupt

(http://docs.python.org/tutorial/errors.html chap. 8.6)

and in some differents code, like Django or Google code, you can see this :

  def AuthenticateAndRun(self, username, password, args):
    raise NotImplementedError()

(http://code.google.com/p/neatx/source/browse/trunk/neatx/lib/auth.py)

The exception is instanciate before being raised while there is no argument.
What is the purpose to instanciate an exception without arguments ? When I should use the first case or the second case ?

Thanks in advance
Fabien

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

述情 2024-09-17 04:45:13

您可以使用任何您喜欢的形式。没有真正的区别,并且在 Python 2 和 3 中都是合法的。 Python 风格指南 没有具体说明推荐哪一种。


关于“类形式”支持的更多信息:

try:
    raise KeyboardInterrupt

这种形式在 Python 2 和 3 中都是完全合法的。
摘自 pep-3109

raise EXCEPTION 用于引发新的异常。这个表格有两个
子变体:EXCEPTION 可以是异常类或异常类的实例
异常类;有效的异常类是 BaseException 及其
子类[5]。 如果 EXCEPTION 是子类,则调用它时不会出现任何异常
获取异常实例的参数。

Python 文档

...如果是类,需要时会获取异常实例
通过实例化不带参数的类。

You can use whichever form you like. There is no real difference and both are legal in Python 2 and 3. Python style guide does not specify which one is recommended.


A little more information on the "class form" support:

try:
    raise KeyboardInterrupt

This form is perfectly legal in both Python 2 and 3.
Excerpt from pep-3109:

raise EXCEPTION is used to raise a new exception. This form has two
sub-variants: EXCEPTION may be an exception class or an instance of an
exception class; valid exception classes are BaseException and its
subclasses [5]. If EXCEPTION is a subclass, it will be called with no
arguments to obtain an exception instance.

It is also described in Python documentation:

... If it is a class, the exception instance will be obtained when needed
by instantiating the class with no arguments.

凉栀 2024-09-17 04:45:13

引发异常类而不是异常实例是不推荐使用的语法,不应在新代码中使用。

raise Exception, "This is not how to raise an exception..."

Raising an exception class instead of an exception instance is deprecated syntax and should not be used in new code.

raise Exception, "This is not how to raise an exception..."
御弟哥哥 2024-09-17 04:45:13

在 C++ 等语言中,您可以引发任何对象,而不仅仅是异常。 Python 受到更多限制。如果你尝试:

raise 1

你会得到:

Traceback (most recent call last):
...
TypeError: exceptions must be old-style classes or derived from BaseException, not int

在Python编程模型中,你通常可以单独使用类而不是实例(这对于快速创建唯一实例很方便,只需定义一个类)。因此,难怪您可以引发异常类而不是异常实例。

然而,正如伊格纳西奥所说,这已被弃用。

另外,一些旁注不是直接问题:

您还可以在某些代码中单独看到 raise 本身。没有任何对象类或任何东西。它在异常处理程序中用于重新引发当前异常并保留初始回溯。

In languages like C++ you can raise any object, not just Exceptions. Python is more constrained. If you try :

raise 1

You get:

Traceback (most recent call last):
...
TypeError: exceptions must be old-style classes or derived from BaseException, not int

In python programming model you can usually use a class by itself instead of an instance (this is handy for fast creation of unique instances, just define a class). Hence no wonder you can raise an exception class instead of an exception instance.

However like Ignacio said, that is deprecated.

Also, some side-note that is not the direct question:

You could also see raise alone by itself in some code. Without any object class or anything. It is used in exception handlers to re-raise the current exception and will keep the initial traceback.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文