(PP-5)

5A.Write a python program for handling zerodivision error exception.

def this_fails():

    try:

        x=1/0

    except ZeroDivisionError as err:

        print("Handling runtime error:",err)

    finally:

        print("Executing finally clause")

this_fails()

5B.Write a python program for handling keyerror exception.

def keyErr():
    try:
        dict={1:100,2:200}
        print(dict[2])
    except KeyError as err:
        print("Key error Exception",err)
    finally:
        print("executing finally clause")
keyErr()

5C.Write a python program for handling ioerror exception.

def IOErr():
    try:
        fout=open("file2.txt","w")
        str=fout.read()
        print(str)
    except IOError as err:
        print("IO error exception",err)
    finally:
        print("executing finally clause")
IOErr()

5D.Write a python program for handling indexerror exception.

def indexErr():
    try:
        list=[1,2,3,4]
        print(list[4])
    except IndexError as err:
            print("index error exception:",err)
    finally:
            print("executing final clause")
indexErr()   

5E.Write a python program for handling importerror exception.

def importErr():
    try:
        import module_does_not_exist
    except ImportError:
            print("import Error Exception")
    finally:
            print("Executing finally clause")
importErr()
            OR
def importErr():
    try:
        import maths
    except ImportError as err:
        print("Handling Import Error-",err)
        print("import Error Exception")
    finally:
        print("Executing finally clause")
importErr()




Comments

Popular posts from this blog

python(BI)

Prac_8(AMP)

LSA10