Types Of Errors And Exceptions In Python

NameError, TypeError, ValueError, zeroDivisionError, OSError, IndexError

Rohit Kumar Thakur
4 min readJan 8, 2022

--

Errors And Exception: Python tutorial For Beginners

Hello Learners..!!

In the last section of this Python Tutorial Series, we talked about File Handling In Python. In this article, we are going to handle different types of errors in Python.

You all are programmers, what can I say new about errors? I know errors are annoying. But dealing with those errors makes a programmer perfect. Errors occur when something goes wrong. When we code a simple program in python then syntax errors and exceptions errors are the most common errors we face. For example:

>>> print('Hello world)
File "<stdin>", line 1
print('Hello world)
^
SyntaxError: unterminated string literal (detected at line 1)
>>> for i in range(0,24)
File "<stdin>", line 1
for i in range(0,24)
^
SyntaxError: expected ':'

A syntax error occurs when a rule of Python grammar is violated. In the first example, we missed the quote mark at the end of the string. We miss a colon in the for loop which results in an error.

In contrast to a syntax error, an exception occurs because of some mistake in the program that the system cannot detect before executing the code as there is nothing wrong in terms of the syntax but leads to a situation during the program execution that the system cannot handle. For example, opening a non-existing file.

If you are a fan of Mr. Robot then you may heard these lines about errors:

A bug is never just a mistake. It represents something bigger. An error of thinking that makes you who you are.

So, In this article, we will talk about the type of errors we face in Python programming.

Commonly encountered Exceptions:

  • NameError
>>> names = Input('Enter your name: ')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'Input' is not defined. Did you mean: 'input'?

This exception occurs whenever a name that appears in a statement is not found globally. In the above example, the Python aspects us to type “input” but instead of this we typed “Input”. Python being a case-sensitive fails to recognize the function Input and the system responds with an error message.

  • TypeError
>>> sums = 2 + '5'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'

This exception occurs when an operation or function is applied to an object of an inappropriate type. The above TypeError is the perfect example of this exception.

  • ValueError
>>> int('Hello')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'Hello'

This exception occurs whenever an inappropriate argument value, even though of the correct type, is used in a function call.

  • ZeroDivisonError
>>> 84 /(5-4-1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero

As the name suggests, In the division method, whenever the denominator is zero. This type of exception occurs.

  • OSError
>>> a = open('abc.txt')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'abc.txt'

This exception occurs when a system-related error occurs. The errors can be anything like disk full or opening a non-existing file, an error related to I/O.

  • IndexError
>>> number = [11,45,68,69]
>>> number[3]
69
>>> number[4]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range

This exception occurs whenever we try to access an index that is out of valid range.

That’s it for this article. If I missed something then let me know in the comment section.

If this article sounds informative to you then make sure to follow and clap. Share it with your geek community.

Attention all developers seeking to make social connections and establish themselves while earning passive income — look no further! I highly recommend ‘From Code to Connections’, a book that will guide you through the process. Don’t miss out, grab your copy now on Amazon worldwide or Amazon India! You can also go for Gumroad

Happy Hacking..!!

Hello, My Name is Rohit Kumar Thakur. I am open to freelancing. I build React Native projects and I am currently working on Python Django. Feel free to contact me at (freelance.rohit7@gmail.com).

--

--