EXCEPTION HANDLING-PYTHON 2022

EXCEPTION HANDLING;

                          Unexpected, Unwanted event that disturbs the normal flow of the program is called exception. Mainly errors are classified into two types and they are classified as follows:

  • SYNTAX ERRORS
  • RUNTIME ERRORS

Whenever exception raised program will terminated abnormally and remaining code will not execute. We have to handle the exception for normal termination of the program. We can handle exception using try, except block. You can write exceptional code in try block and handling code in except block.we can use try except unlimited in a program.

Without try, except block:

Ex-1:

print(“hello”)

print(10/0)

print(“how are you”)

print(“program ends”)

 

With try, except block:

Ex-2:

try:

       n=int(input(“Enter a number”)

        print(10/n)

except ZeroDivisionError as a:

                print(10/5)

                print(“how are you”)

                print(“program ends”)





If exception not matched with except block then exception raised.

Ex-1:

try:

                n=int(input(“Enter a number”))

                print(10/n)

exceptValueError as  a:

                print(a)

                print(“how are you”)

print(“program ends”)

 

In between try, except block statement declarations are not allowed.

Ex-1:

try:

                n=int(input(“Enter a number”))

                print(10/n)

                print(“abc”)

exceptValueError as a:

                print(a)

                print(“how are u”)

print(“program ends”)

 

Single try block not allowed and single except block not allowed.

Ex-1:

try:

                print(“outer try”)

                n=int(input(“enter a value”))

print(10/n)

try:

                x=eval(input(“Enter 4 elements”))

                y=int(input(“Enter index values”))

                print(x[y])

except IndexError as a:

                print(a)

else:

                print(“inner else”)

except ZeroDivisionerror as z:

                print(z)

else:

                print(“outer else block”)

print(“program ends”)

 

Finally Block

This finally block will execute always just before termination of the program, either normally or abnormally.

Ex-1:

try:

                n=int(input(“Enter a number”))

                print(10/n)

except ZeroDivisionError as a:

                print(“except block”)

finally:

                print(“finally”)



Try block will not be executed in 2 cases:

CASE-1:

If exception raised outside of the try block then finally block will not be executed.

Ex-1:

n=int(input(“Enter a number”))

print(10/n)

try:

                print(“try”)

except ZeroDivisonError as a :

                print(10/0)

finally:

                print(“finally”)

 

CASE-2:

Whenever exit() executed a program will terminated automatically in these case finally block will not be executed.

Ex-1:

import os

try:

     n=int(input(“Enter a number”))

     if n==0:
  1. _exit(0)
           print(5/n)

except ZeroDivisionError  As A:

      print(10/0)

finally:

       print(“finally”)

 

 

 

 

 

 

Related Posts