15.1. Exception About

  • Used when error occurs

  • You can catch exception and handles erroneous situation

  • If file does not exists

  • If no permissions to read file

  • If function argument is invalid type (ie. int('one'))

  • If value is incorrect (ie. negative Kelvin temperature)

  • If network or database connection could not be established

15.1.1. NameError

  • Local or global name is not found

>>> print(firstname)
Traceback (most recent call last):
NameError: name 'firstname' is not defined

15.1.2. IndexError

  • Sequence subscript is out of range

>>> data = ['Mark', 'Melissa', 'Rick']
>>>
>>> data[10]
Traceback (most recent call last):
IndexError: list index out of range

15.1.3. KeyError

  • Dictionary key is not found

>>> data = {'firstname': 'Mark', 'lastname': 'Watney'}
>>>
>>> data['age']
Traceback (most recent call last):
KeyError: 'age'

15.1.4. SyntaxError

  • Parser encounters a syntax error

>>> if True
...     print('Mark')
...
Traceback (most recent call last):
SyntaxError: expected ':'

15.1.5. IndentationError

  • Syntax errors related to incorrect indentation

>>> if True:
...     print('Mark')
...      print('Melissa')
...     print('Rick')
...
Traceback (most recent call last):
IndentationError: unexpected indent

15.1.6. ValueError

  • Argument has an invalid value

>>> float('1')
1.0
>>> float('one')
Traceback (most recent call last):
ValueError: could not convert string to float: 'one'

15.1.7. TypeError

  • Operation or function is applied to an object of inappropriate type

>>> data = ['Mark', 'Melissa', 'Rick']
>>>
>>> float(data)
Traceback (most recent call last):
TypeError: float() argument must be a string or a real number, not 'list'

15.1.8. AttributeError

  • Attribute reference or assignment fails

>>> data = ('Mark', 'Melissa', 'Rick')
>>>
>>> data.append('Alex')
Traceback (most recent call last):
AttributeError: 'tuple' object has no attribute 'append'

15.1.9. IsADirectoryError

  • Trying to open directory instead of file

>>> open('/tmp')
Traceback (most recent call last):
IsADirectoryError: [Errno 21] Is a directory: '/tmp'

15.1.10. FileNotFoundError

  • File does not exists

>>> open('myfile.txt')
Traceback (most recent call last):
FileNotFoundError: [Errno 2] No such file or directory: 'myfile.txt'

15.1.11. ModuleNotFoundError

  • Module could not be located

>>> import nasa
Traceback (most recent call last):
ModuleNotFoundError: No module named 'nasa'

Note, that this exception is also raised when you don't have this module installed. Such as while importing pandas or numpy without installing it first.

15.1.12. ZeroDivisionError

  • Division or modulo by zero

>>> 1 / 0
Traceback (most recent call last):
ZeroDivisionError: division by zero
>>> data = []
>>>
>>> mean = sum(data) / len(data)
Traceback (most recent call last):
ZeroDivisionError: division by zero

15.1.13. KeyboardInterrupt

  • User interrupts program execution

  • Usually by pressing Ctrl+C

  • PyCharm: Stop button in the Python Console toolbar

While running the following code, you can interrupt it by pressing Ctrl+C:

>>> 
... while True:
...     pass
...
Traceback (most recent call last):
KeyboardInterrupt