Why are Python exceptions named "Error" (e.g. ZeroDivisionError
, NameError
, TypeError
) and not "Exception" (e.g. 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.
Is there a difference in Python also or not?
NameErrors are raised when your code refers to a name that does not exist in the current scope. For example, an unqualified variable name. The given code is rewritten as follows to catch the exception and find its type.
Python Logical Errors (Exceptions)Errors that occur at runtime (after passing the syntax test) are called exceptions or logical errors.
An Error might indicate critical problems that a reasonable application should not try to catch, while an Exception might indicate conditions that an application should try to catch. Errors are a form of an unchecked exception and are irrecoverable like an OutOfMemoryError , which a programmer should not try to handle.
The NameError can be avoided by using a technique called Exception Handling. Even if we write code without any SyntaxError, the program can result in runtime errors. These are called Exceptions. There are numerous built-in exceptions available in Python, and One such exception is NameError Exception.
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.
Not all Exceptions are Errors. SystemExit
, KeyboardInterrupt
, StopIteration
, GeneratorExit
are all exceptions and not errors. The word 'Error' in actual errors shows the difference.
'Error' is shorter than 'Exception'. That can save a few characters in the code width with no loss in meaning. That makes some difference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With