Python Menu

Exception handling in Python


Python provides us with the way to handle the Exception so that the other part of the code can be executed without any interrupt.

For Exception handling, python uses following keywords or statements

  • try
  • except
  • finally
  • raise
  • assert

try – except statement

If the python program contains suspicious code that may throw the exception, we must place that code in the try block. The try block must be followed with the except statement which contains a block of code that will be executed if there is some exception in the try block.

Syntax:
try:  
    #block of code   
except Exception1:  
    #block of code  
except Exception2:  
    #block of code  
#other code

Example:
try:  
    a = int(input("Enter a:"))  
    b = int(input("Enter b:"))  
    c = a/b;  
    print("a/b = %d"%c)  
except Exception:  
    print("can't divide by zero")
#other code:  
print("other part of the program")
Output: Case 1
Enter a:23
Enter b:3
a/b = 7
other part of the program
Output: Case 2
Enter a:3
Enter b:0
can't divide by zero
other part of the program
Output: Case 3
Enter a:34
Enter b:abc
can't divide by zero
other part of the program

try – except - else statement

We can also use the else statement with the try-except statement in which, we can place the code which will be executed if no exception occurs in the try block.

Syntax:
try:  
    #block of code   
except Exception1:  
    #block of code   
else:  
    #this code executes if no except block is executed

Example:
try:  
    a = int(input("Enter a:"))  
    b = int(input("Enter b:"))  
    c = a/b;  
    print("a/b = %d"%c)  
except Exception:  
    print("can't divide by zero")
else:
	print("code for else block")
#other code:  
print("other part of the program")
Output: Case 1
Enter a:21
Enter b:3
a/b = 7
code for else block
other part of the program
Output: Case 2
Enter a:3
Enter b:0
can't divide by zero
other part of the program
Output: Case 3
Enter a:3
Enter b:df
can't divide by zero
other part of the program

finally statement

We can use the finally block with the try block in which, we can place the important code which must be executed either try throws an exception or not.

Syntax:
try:  
    #block of code   
 except Exception1:  
    #block of code   
 else:  
    #this code executes if no except block is executed 
finally:
     # this code  will always be executed


Example:
try:  
    a = int(input("Enter a:"))  
    b = int(input("Enter b:"))  
    c = a/b;  
    print("a/b = %d"%c)  
except ZeroDivisionError:  
    print("can't divide by zero")
except ValueError:
	print("Enter Numbers only")
else:
	print("code for else block")
finally:
	print("Imp code-always executes")
Output: Case 1
Enter a:24
Enter b:4
a/b = 6
code for else block
Imp code-always executes
Output: Case 2
Enter a:3
Enter b:0
can't divide by zero
Imp code-always executes
Output: Case 3
Enter a:36
Enter b:abc
Enter Numbers only
Imp code-always executes

Next Topic :Raise & Assert Statements in Python