views:

209

answers:

5

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

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 error and exception.

Is there a difference in Python also or not? Can someone explain or point me to some documentation explaining it?

Thank you!

+1  A: 

Disclaimer: I don't know exactly. I'd guess its convention - in the sense that an Exception can have any name, and the names are chosen to sound meaningful.

There are even exceptions that are called StopIteration or GeneratorExit. I guess these examples sound more concise than StopIterationException or GeneratorException.

The MYYN
I belived that too but then this popped up when defining exceptions (from the Python tutorial): class MyError(Exception). You name it error but the base class is exception ?!?
Elena
Most of the time you'll name your custom Exceptions with suffix Exception. But, well, there can be exceptions.
The MYYN
+1  A: 

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.

gustafc
The base class is called Exception (see my comment on the other answer) so why not name the children Exception too?
Elena
@Elena: Because then you would finally end up with something like `class ExceptionHandlingException extends RuntimeException` and that's just horrible to read.
Esko
Apart from other reasons posted by others, keep in mind the naming in the Python standard library isn't always consistent.
gustafc
+8  A: 
  1. You don't name each class with 'Class' in name and each variable with '_variable' in name. The same name you don't name exception using the word 'Exception'. The name should tell something about the meaning of the object. 'Error' is the meaning of most of the 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'. Than can save a few characters in the code width with no loss in meaning. That makes some difference.

Jacek Konieczny
+3  A: 

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

extraneon
+3  A: 

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

daotoad