Exception Handling and import libraries in python:

 Exception Handling and import libraries in python:

Exception Handling

We would have gone through so many bugs and errors in our journey of learning programs. This helps us learn many lessons and to become a good programmer.

There are two types of errors in Python. They are syntax error and exceptions. 

Difference between syntax error and exceptions:

We have many rules in the programming language like indentation and the syntax. When we don’t follow it we get syntax error. 

But when the syntax is correct and when we get some other errors when we try to execute, then those errors are called exceptions. Handling those errors are called as exception handling.

Difference between syntax error and exceptions

Difference between syntax error and exceptions

Commonly occurring exceptions:

ZeroDivisionError: When you try to divide a number by zero

ImportError: When you try to import library which is not installed or when the path given is wrong.

IndexError: When the index is not found.

IndentationError: When the indentation is not proper

ValueError: When the arguments passes are of invalid datatype

Exception Handling by try and except:

Exception can be handled by try and except. The statement will execute either try or except. If there is no exception try will be executed and if there is exception except will be executed.


Exception handling in try and except

Import Error in python:

When you import libraries when you haven’t installed that particular library or the given path is not correct it’ll raise import error. 


Import Error in python

Post a Comment