java 为什么 Python 异常名为“Error”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2903827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 23:21:53  来源:igfitidea点击:

Why are Python exceptions named "Error"?

javapythonexception

提问by Elena

Why are Python exceptions named "Error" (e.g. ZeroDivisionError, NameError, TypeError) and not "Exception" (e.g. ZeroDivisionException, NameException, TypeException).

为什么 Python 异常被命名为“错误”(例如ZeroDivisionError, NameError, TypeError)而不是“异常”(例如ZeroDivisionException, NameException, TypeException)。

I come from a Java background and started to learn Python recently, as such this is confusing because in Java there is a distinction between errors and exceptions.

我来自 Java 背景,最近开始学习 Python,因此这很令人困惑,因为在 Java 中,错误和异常之间是有区别的。

Is there a difference in Python also or not?

Python 也有区别吗?

回答by Jacek Konieczny

  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, GeneratorExitare 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.

  1. 您不会在名称中使用“Class”来命名每个类,而在名称中使用“_variable”来命名每个变量。与您不使用“异常”一词命名异常的方式相同。名称应该说明对象的含义。“错误”是大多数异常的含义。

  2. 并非所有异常都是错误。SystemExit, KeyboardInterrupt, StopIteration,GeneratorExit都是例外而不是错误。实际错误中的“错误”一词显示了差异。

  3. “错误”比“异常”短。这可以在代码宽度中节省几个字符而不会丢失含义。这有点不同。

回答by daotoad

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

我相信这个约定来自PEP 8 - Python 代码风格指南

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).

异常名称

因为异常应该是类,类命名约定在这里适用。但是,您应该在异常名称上使用后缀“Error”(如果异常实际上是错误)。

回答by extraneon

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

在这方面,Python 与 Java 非常相似。但是 Python 的 Exception 应该与 Java 的 Throwable 进行比较。

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

由于 Throwable 有各种各样的风格——错误、运行时异常和(检查的)异常——Python 也是如此(尽管没有检查的异常)。

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

对于语言来说,Error 异常,所以继承层次并不奇怪。

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.

不过,我并不特别喜欢 Exception 这个名字。异常不仅用于异常情况(如希望出现错误),而且还用于脱离控制流。因为这就是 Exception 的作用;它跳出正常的控制流到一个标记点​​。有点像 goto,但更精致。

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.

也就是说,每次遇到找不到合适的返回值的情况时,您倾向于使用异常。无论是在 Python 中还是在 Java 中。

回答by DavidRR

Q. Why are Python exceptions named “Error”?

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

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

我推测这是因为大多数Python 异常被归类为错误警告。如果 Python 异常的名称以 结尾Exception,则无法进行这种区分。

Examples of warnings are DeprecationWarningand ImportWarning.

警告的例子是DeprecationWarningImportWarning

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

请参阅2.x 类层次结构的内置异常以及3.x

回答by Xavier

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 Exceptioncan also be raised directly
  • Python 异常未命名为“错误”。
  • Python 错误被命名为“错误”。
  • Python 错误可以作为异常引发、捕获和处理。
  • 以错误开始的事情可能最终成为不会导致错误消息的已处理异常。
  • 一个Exception也可以直接提出

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

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

Errors detected during execution are called exceptionsand 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.
  • 该程序监视错误。
  • 如果发生错误但程序在执行期间未检测到错误,则会导致错误消息。
  • 如果在执行过程中发生错误并被程序检测到,则为异常。
  • 程序可以处理异常。它们可以被优雅地处理或导致错误消息。
  • 程序未处理的异常是未处理未捕获)异常并成为错误消息。

回答by gustafc

It's just naming. In Java, you have java.lang.Errordistinct 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.

这只是命名。在 Java 中,您java.lang.Error与其他Throwables不同,因为需要取消检查这些类型的错误。在 Python 中,所有异常都是未经检查的,因此这种区分是毫无意义的。