Python Try - Except

It is used for error handling in python, try is used for caught and except is used to handle these errors.


Python has some built-in exceptions which stop the program execution but these not affect the output result, so to reduce these error use try-except, which handle the process and program running properly.

There are two types of error which are handled by this:

  • Syntax Error

  • Exception

The program contains several types of exception which are handled by this:

  • IOError 

  • KeyboardInterrupt

  • ValueError 

  • EOFError 

  • ImportError 

By this program divided into the three parts:

  • try

  • Except

  • finally

We can easily understand it with the help of the given below example:

>>> Import sys       #use to get the type of exception


>>> list = [1,2,3]

>>> for a in list:

>>>     try:

>>>         print(“value is”)

>>>         result = a

>>>         break

>>>     catch:

>>>         print(“enter another value”)

>>> print(“the result is”, a)    

Now Python's basic topics are covered in the next tutorial we will cover the most important topic Python File Handling. If you have not read the previous tutorials then please first read all the video and go to the next topics file handling.