使用 Python 异常层次结构
Using the Python exception hierarchy
Python 提供广泛的 list of built-in exceptions.
在我看来,大多数内置异常只能由解释器或 Python 的内置函数引发。
那么,这些内置异常中的哪些应该在用户代码中引发? Python 2.7 和 3.x 之间的指南是否不同?
来自 .NET,我认为这是一个重要的问题。对于 .NET,Microsoft 提供了关于抛出哪些异常以及 不 抛出 Using Standard Exception Types.
的明确指导
对于 Python,在我看来,从直觉上和我看到的示例来看,以下内置异常适合在用户代码中引发:
IndexError
LookupError
NotImplementedError
TypeError
ValueError
...但是这些 不会 因为每个都表示一种系统故障:
ArithmeticError
FloatingPointError
OverflowError
ZeroDivisionError
MemoryError
OSError
Python 程序员在处理用户代码引发的异常方面拥有很大的灵活性。然而,这种灵活性并不意味着“一切皆有可能”。在 Python 标准库的第 3 版文档中,文章 5. Built-in Exceptions 提供了这个一般指导。
程序员可以提出哪些异常:
User code can raise built-in exceptions. This can be used to test an
exception handler or to report an error condition “just like” the
situation in which the interpreter raises the same exception; but
beware that there is nothing to prevent user code from raising an
inappropriate error. (emphasis mine)
关于程序员定义的异常:
The built-in exception classes can be subclassed to define new
exceptions; programmers are encouraged to derive new exceptions from
the Exception class or one of its subclasses, and not from
BaseException. More information on defining exceptions is
available in the Python Tutorial under User-defined Exceptions.
子句 5.1. Base classes 建议 base class 异常 通常 不 被引发:
The following exceptions are used mostly as base classes for other
exceptions.
BaseException, Exception, ArithmeticError, BufferError, LookupError
相反,根据条款 5.2. Concrete exceptions 中的指导,具体例外情况 通常 应该 提出:
The following exceptions are the exceptions that are usually raised.
AssertionError, AttributeError, EOFError, ... OSError, ..., ZeroDivisionError
因此,例如,与其引发 ArithmeticError,不如考虑引发其派生的 classes 之一:FloatingPointError, OverflowError 和 ZeroDivisionError。 (或者,可能是您从 ArithmeticError 派生的 class。)
(有关 OSError 的更多信息,请参见子句 5.2.1. OS exceptions。)
最后,关于文章 29.5. warnings — Warning control 中的 警告消息 :
Warning messages are typically issued in situations where it is useful
to alert the user of some condition in a program, where that condition
(normally) doesn’t warrant raising an exception and terminating the
program. For example, one might want to issue a warning when a program
uses an obsolete module.
...
User code can define additional warning categories by subclassing one
of the standard warning categories. A warning category must always be
a subclass of the Warning class.
Python 提供广泛的 list of built-in exceptions.
在我看来,大多数内置异常只能由解释器或 Python 的内置函数引发。
那么,这些内置异常中的哪些应该在用户代码中引发? Python 2.7 和 3.x 之间的指南是否不同?
来自 .NET,我认为这是一个重要的问题。对于 .NET,Microsoft 提供了关于抛出哪些异常以及 不 抛出 Using Standard Exception Types.
的明确指导对于 Python,在我看来,从直觉上和我看到的示例来看,以下内置异常适合在用户代码中引发:
IndexError
LookupError
NotImplementedError
TypeError
ValueError
...但是这些 不会 因为每个都表示一种系统故障:
ArithmeticError
FloatingPointError
OverflowError
ZeroDivisionError
MemoryError
OSError
Python 程序员在处理用户代码引发的异常方面拥有很大的灵活性。然而,这种灵活性并不意味着“一切皆有可能”。在 Python 标准库的第 3 版文档中,文章 5. Built-in Exceptions 提供了这个一般指导。
程序员可以提出哪些异常:
User code can raise built-in exceptions. This can be used to test an exception handler or to report an error condition “just like” the situation in which the interpreter raises the same exception; but beware that there is nothing to prevent user code from raising an inappropriate error. (emphasis mine)
关于程序员定义的异常:
The built-in exception classes can be subclassed to define new exceptions; programmers are encouraged to derive new exceptions from the Exception class or one of its subclasses, and not from BaseException. More information on defining exceptions is available in the Python Tutorial under User-defined Exceptions.
子句 5.1. Base classes 建议 base class 异常 通常 不 被引发:
The following exceptions are used mostly as base classes for other exceptions.
BaseException, Exception, ArithmeticError, BufferError, LookupError
相反,根据条款 5.2. Concrete exceptions 中的指导,具体例外情况 通常 应该 提出:
The following exceptions are the exceptions that are usually raised.
AssertionError, AttributeError, EOFError, ... OSError, ..., ZeroDivisionError
因此,例如,与其引发 ArithmeticError,不如考虑引发其派生的 classes 之一:FloatingPointError, OverflowError 和 ZeroDivisionError。 (或者,可能是您从 ArithmeticError 派生的 class。)
(有关 OSError 的更多信息,请参见子句 5.2.1. OS exceptions。)
最后,关于文章 29.5. warnings — Warning control 中的 警告消息 :
Warning messages are typically issued in situations where it is useful to alert the user of some condition in a program, where that condition (normally) doesn’t warrant raising an exception and terminating the program. For example, one might want to issue a warning when a program uses an obsolete module.
...
User code can define additional warning categories by subclassing one of the standard warning categories. A warning category must always be a subclass of the Warning class.