Why are Python exceptions called "Error"?

Why are Python exceptions called β€œError” (for example, ZeroDivisionError , NameError , TypeError ) and not β€œException” (for example, ZeroDivisionException , NameException , TypeException ).

I came from the Java background and started learning Python recently, as it is confusing because in Java there is a difference between errors and exceptions.

Is there any difference in Python?

+60
java python exception
May 25 '10 at 10:36
source share
6 answers
  • You do not name each class "Class" in the name and each variable named "_variable". You do not call the same name an exception, using the word "Exception". The name must say something about the meaning of the object. β€œError” is the meaning of most exceptions.

  • Not all exceptions are errors. SystemExit , KeyboardInterrupt , StopIteration , GeneratorExit - all exceptions, not errors. The word "error" in actual errors shows the difference.

  • The error is shorter than the exception. Than you can save several characters in the width of the code without losing meaning. It matters.

+70
May 25 '10 at 10:58 a.m.
source share

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

Exception Names

Since exceptions must be classes, the class naming convention applies here. However, you must use the suffix "Error" on yours (if the exception is really an error).

+41
May 25 '10 at 17:20
source share

In this regard, Python is pretty similar to Java. But the Python exception should be compared to Java Throwable.

As Throwables come in all sorts of flavors β€” Error, RuntimeException, and (checked) Exception β€” Python does the same (though not checked exceptions).

As for the language, the error is exceptional, so the inheritance hierarchy is not strange.

I especially don't like the name Exception. Exceptions are used not only for exceptional circumstances (for example, we hope errors), but also in order to simply exit the control flow. Because this is what makes the Exception; he jumps out of the normal control flow to the marked point. A bit like goto, but more sophisticated.

However, every time you have a situation where a suitable return value cannot be found, you usually use an exception. Both in Python and in Java.

+8
May 25 '10 at 11:04 a.m.
source share

Q. Why are Python exceptions called "Error"?

I assume that most Python exceptions are classified as errors or warnings . If Python exception names ended in Exception , this distinction would not have been possible.

Examples of alerts are DeprecationWarning and ImportWarning .

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

+4
Mar 10 '15 at 18:41
source share

This is just naming. In Java, you have java.lang.Error different from other Throwable , because these errors must be disabled. In Python, all exceptions are not marked, so the difference does not make sense.

+1
May 25 '10 at 10:46
source share

Simply put:

  • Python exceptions are NOT called Error.
  • Python errors are called Error.
  • Python errors can be raised, caught, and treated as exceptions.
  • Something starting as an error may turn out to be an exception that does not result in an error message.
  • Exception can also be raised directly

Concept:

I usually do this, but I'm going to make an exception

OR

This is usually a mistake, but we are going to throw an exception, catch it and perform some procedure.

Details:

Exceptions against errors:

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

Errors detected at runtime are called exceptions and are not unconditionally fatal

Procedure:

  • The program tracks errors.
  • If an error occurs, but is NOT detected by the program at run time, this results in an error message.
  • If an error occurs and is detected by the program at runtime, this is an exception.
  • Exceptions may be thrown by the program. They can be processed gracefully or lead to an error message.
  • Exceptions that are NOT handled by the program are unhandled (non-displayed) exceptions and become error messages.
0
Nov 29 '18 at 21:55
source share



All Articles