Home » Python

Exceptions handling in Python

Python Exception Handling: In this tutorial, we will learn about the python exceptions and how to handle exceptions in Python programming language?
Submitted by Bipin Kumar, on October 20, 2019

Exception in Python

An exception is a Python object that represents error that occurs during the execution of the program and this disturbs the flow of a program. In general, if Python does not encounter any scripts or lines of code then it raises a type of exception.

Common Exceptions in Python

Some of the most common built-in exceptions in Python are,

  1. ValueError:
    This type of error occurs when we have written an argument or line of code that has the right type but inappropriate value. For example, a variable is assigned with the string and we are trying to convert it into integer then we will get this type of error.
  2. ZeroDivisionError:
    This is the most common type of error which occurs when we are trying to divide or taking modulo of any integer by the Zero. For example, a variable x by 5 and we are trying to do operation x%0 or x/0 then this shows us ZeroDivisionError.
  3. ImportError:
    This error occurs when we are trying to import a module in the program but it not found. For example, if we are importing a module like Bipin in the program then it shows this type of error.
  4. IndentionError:
    As we all know that the Python follows the rule of indentation. So, when we are going to write something out of indentation then this type of error will occur.
  5. SyntaxError:
    This error occurs when we have written any incorrect syntax in the program. As we all know that the keyword 'int' is used to convert the input into an integer type but when we are trying to use 'Int' instead of 'int' then it will show the SyntaxError.
  6. IndexError:
    This type of error occurs when you are trying to access any value which is out of the given range. For example, we have created a list of lengths of 5 but we are trying to access the element at index 6 then it will show the IndexError.
  7. MemoryError:
    This type of error occurs when an operation runs out of memory.

To overcome these types of exceptions in python programming language, we use the try-except statements.

The basic syntax of try-except statement is,

try:
   #statement
except typeofError:
   #statement

Algorithm of try-except statement

  • If the code or statement provided in the try block has no exception then only try blocks will be executed.
  • If any exception occurs in the block of try then try block skipped and except block will be executed.

Now, we will see an example of exception handling by using the try-except statement in Python.

# input two integer numbers
a=int(input('Enter value of a: '))
b=int(input('Enter value of b: '))

try:
    print('a/b=',a/b)
except ZeroDivisionError:
    print("Sorry, wrong value of the denominator.")

Output

RUN 1:
Enter value of a: 3
Enter value of b: 5
a/b= 0.6

RUN 2:
Enter value of a: 3
Enter value of b: 0
Sorry, wrong value of the denominator.





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.