Tuesday, 28 January 2020

Nested try block and else block


try:
    print("outer try block")
    try:
        print("inner try block")
        print(2/0) #exception raises here, and corresponding except is matched
    except ZeroDivisionError:
        print("inner except block")
    finally:
        print("inner finally block")
except:
    print("outer except block")
finally:
    print("outer finally block")

"""output:
outer try block
inner try block
inner except block
inner finally block
outer finally block
>>>
"""
We can take try-except-finally blocks inside try or except or finally blocks it is called nesting exception handling.
Generally risky code we have to take inside outer try block and too much risky code we have to take inside inner try block.

·       Inside inner try block if an exception raised then except block is responsible to handle.  If it unable to handle then outer except block is responsible to handle.

Syntax:
try:
      ……
      ……
      try:
       ……..
       ………
       except:
       ……..
       ………
except:
      ……
      ……

#example program on nested try-except finally block
try:#outer try block
    print("statement1")
    print(4/2)#statement2 5.0
    print(10/5)#statement3 5.0
    try: #inner try block
        a=int(input("enter a value ")) #4
        b=int(input("enter b value ")) #"two"
        print("statement 4")
        print(a/b)#statement 5 #exception is raised
        print("statement 6")
    except ValueError:#inner exception block
        print("enter int only")#statement 7
    finally:#inner finally block
        print("statement 8")
        print("statement 9")
except ZeroDivisionError: #outer except block
        print("Zero Division is not possible")#statement 10
finally:
    print("statement 11")
print("statement 12")
"""
output:
statement1
2.0
2.0
enter a value 4
enter b value 0
statement 4
statement 8
statement 9
Zero Division is not possible
statement 11
statement 12
case 2:
statement1
2.0
2.0
enter a value 4
enter b value two
enter int only
statement 8
statement 9
statement 11
statement 12

"""
else block with try-except finally block
#example progrm on else block with try-except finally block
try:
    a=int(input("enter a value "))
    b=int(input("enter b value "))
    print("hi")
    print(a/b)
except ZeroDivisionError:
    print("except block")
else:
    print("else block")
finally:
    print("finally block")
"""output:
Case 1:
enter a value 2
enter b value 1
hi
2.0
else block
finally block
case 2:
enter a value 4
enter b value 0
hi
except block
finally block
"""
 We can use else block with try-except finally blocks. Else block will be executed if and only if there are no exception inside try block.

Syntax:
try:
          Risky code:
except:
          It executes if any exception occur in the try block
else:
          It executes if there is no exception in the try block
finally:
          It executes always
Note:
1.   For every else block, except block is mandatory.
Example:
try:
    a=int(input("enter a value "))
    b=int(input("enter b value "))
    print("hi")
    print(a/b)
else:  #Error
    print("else block")
2.   Order of try-except-else-finally is important
3.   Without try block, we can not write finally block
Example:
try:
….
finally:
 ……


Nexthttps://youtu.be/UORaySj-M2w
assert keyword

Previous:


No comments:

Post a Comment

Files with Exception handling

#Ask the user to take two input integer values var a,b try:     a=int(input("enter any integer value:"))     b=int(input(&qu...