top of page

Data Scientist Program


Free Online Data Science Training for Complete Beginners.

No prior coding knowledge required!

Exception and Error Handling in Python

Every programmer has faced many errors. Facing bugs and dealing with them is very important to learn more and become a better programmer.

In this post, we will see together the different types of errors and how to deal with them. First of all, we will start by defining the mainly two types of errors. Then, we will understand the different types of exceptions that we can face while coding and how to deal with these situations.

1) Introduction

There are two main types of errors that every one of us can face:

Syntax errors: It occurs when you don't respect the rules while writing your code. For example, when you forget to put a colon ':' after the while statement as shown in the code below:

In the case of a syntax error, the code does not execute until the error is fixed. We can see that the line of error is indicated with an arrow pointing to the place where the error was detected.


Some times, your code syntax can be correct but while execution an error still throwing up. These errors detected during execution are called exceptions and dealing with them is called exception Handling. We will see some of exception types and how to deal with these errors.

2) Different type of Exceptions

There are some native Exceptions and these are some examples:

Name Error: When a name is not found. This error appears.

ZeroDivisionError: It is raised when we try to divide by zero.

ValueError: When an expression or a function receives the correct type but an an inappropriate value.

TypeError: It is thrown when we use an object with the wrong type.

The code below show some examples of these exceptions:


-------------------------------------------------------------NameError   Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_15576/ in <module>
----> 1 print(z)
NameError: name 'z' is not defined

ZeroDivisionError                         Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_15576/ in<module>      
----> 5 print(divide(7,0))~\AppData\Local\Temp/ipykernel_15576/ in divide(a, b)      
1 def divide(a,b):
----> 2     return a/b
      5 print(divide(7,0))
ZeroDivisionError: division by zero

-------------------------------------------------------------ValueError               Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_15576/ in <module>      
      1 import math
      ----> 3 math.sqrt(-1)
ValueError: math domain error

-------------------------------------------------------------TypeError                 Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_15576/ in <module>
----> 1 print(divide('i',10))
~\AppData\Local\Temp/ipykernel_15576/ in divide(a, b)      
      1 def divide(a,b):
      ----> 2     return a/b
      5 print(divide(7,0))
TypeError: unsupported operand type(s) for /: 'str' and 'int'

There are other native exceptions that you can check them here.

3) Handling exceptions

We can handle exceptions using try and except statement. The try clause will be executed first, if no exceptions occurred then the except is skipped.

This is an example of exceptions handling in Python:



Can't divide by Zero





Recent Posts

See All
bottom of page