为什么 Python 异常被命名为“ Error”?

为什么 Python 异常被命名为“ Error”(例如 ZeroDivisionErrorNameErrorTypeError) 而非「例外」(例如 ZeroDivisionExceptionNameExceptionTypeException)。

我来自 Java 背景,最近才开始学习 Python,因为在 Java 中,错误和异常之间是有区别的。

Python 也有区别吗?

18617 次浏览

It's just naming. In Java, you have java.lang.Error distinct from other Throwables because those kinds of errors need to be unchecked. In Python, all exceptions are unchecked, so the distinction is kind of pointless.

  1. You don't name each class with 'Class' in name and each variable with '_variable' in name. The same way you don't name exception using the word 'Exception'. A name should say something about the meaning of an object. 'Error' is the meaning of most exceptions.

  2. Not all Exceptions are Errors. SystemExit, KeyboardInterrupt, StopIteration, GeneratorExit are all exceptions and not errors. The word 'Error' in actual errors shows the difference.

  3. 'Error' is shorter than 'Exception'. That can save a few characters in the code width with no loss in meaning. That makes some difference.

Python is fairly similar to Java in this respect. But Python's Exception should be compared to Java's Throwable.

As Throwables come in all kinds of flavors - Error, RuntimeException and (checked) Exception - so do Python's (though no checked exceptions).

As for the language, an Error is exceptional, so that inheritance hierarchy is not strange.

I don't particularly like the name Exception though. Exceptions are not only used for exceptional circumstances (like hopefully Errors) but also to just get out of the control flow. Because that is what a Exception does; it jumps out of the normal flow of control to a marked point. A bit like a goto, but more refined.

That said, every time you have a situation in which no suitable return value can be found you tend to use an Exception. Both in Python as in Java.

I believe this convention comes from PEP 8 - Style Guide for Python Code:

Exception Names

Because exceptions should be classes, the class naming convention applies here. However, you should use the suffix "Error" on your exception names (if the exception actually is an error).

Q. Why are Python exceptions named “Error”?

I surmise this is because most Python exceptions are classified as either errors or warnings. If the names of Python exceptions were to end with Exception, this distinction would not be possible.

Examples of warnings are DeprecationWarning and ImportWarning.

Please see the the 2.x class hierarchy for built-in exceptions as well as that for 3.x.

Simply put:

  • Python exceptions are NOT named "Error".
  • Python errors are named "Error".
  • Python errors can be raised, caught, and handled as exceptions.
  • Something that begins as an error can end up being a handled exception that does not result in an error message.
  • An Exception can also be raised directly

Concept:

I normally do this thing but I'm going to make an exception

OR

This would normally be an error, but we're going to make an exception, catch it, and performing some procedure.

Details:

Exceptions vs Errors:

https://docs.python.org/2/tutorial/errors.html

Errors detected during execution are called exceptions and are not unconditionally fatal

Workflow:

  • The program monitors for errors.
  • If an error occurs but is NOT detected by the program during execution, it results in an error message.
  • If an error occurs and is detected by the program during execution, it is an exception.
  • Exceptions can be handled by the program. They can be handled gracefully or result in an error message.
  • Exceptions that are NOT handled by the program are unhandled(uncaught) exceptions and become error messages.