Errors are coding issues in a program which may cause it to exit abnormally.
On the contrary, exceptions happen due to the occurrence of an external event which interrupts the normal flow of the program.
Python lay down Try, Except, Finally constructs to handle errors as well as Exceptions. We enclose the unsafe code indented under the try block. And we can keep our fall-back code inside the except block. Any instructions intended for execution last should come under the finally block.
When the value passed to the index operator is greater than the actual size of the tuple or list, Index Out of Range error is thrown by Python.
>>> a = [1,2,3,4]
>>> a[3]
4
>>> a[4]
Traceback (most recent call last):
a[4]
IndexError: list index out of range
>>> b = (1,2,3,4)
>>> b[4]
Traceback (most recent call last):
b[4]
IndexError: tuple index out of range
TypeError: Occurs when the expected type doesn’t match with the given type of a variable.
ValueError: When an expected value is not given- if you are expecting 4 elements in a list and you gave 2.
NameError: When trying to access a variable or a function that is not defined.
IOError: When trying to access a file that does not exist.
IndexError: Accessing an invalid index of a sequence will throw an IndexError.
KeyError: When an invalid key is used to access a value in the dictionary.
We can use dir(__builtin__) will list all the errors in Python.