Tuesday, 28 January 2020

Exception Handling Part 2

#If there is no exception
try:
    print("Statement 1")
    print("Statement 2")
    print("statement 3")
except ZeroDivisionError:
    print("Statement 4")
finally:
    print("Statement 5")
print("Statement 6")

"""output:
Statement 1
Statement 2
statement 3
Statement 5
Statement 6

Note: If there is no exception, all the statements will be executed, but not except block
"""
Case 2:#If there is exception at statement 2
try:
    print("Statement 1")
    print(10/0)#statement 2 : exception
    print("statement 3") #Not executed
except ZeroDivisionError:
    print("Statement 4")
finally:
    print("Statement 5")
print("Statement 6")

"""output:
Statement 1
Statement 4
Statement 5
Statement 6
Note: If any exception raised within the try block, rest of the try block statements are not executed.
"""

Case 3:#If there is an exception at st 2, but the corresponding exception is not matched
try:
    print("Statement 1")
    print(10/0)#statement 2 : exception
    print("statement 3")
except TypeError:
    print("Statement 4")
finally:
    print("Statement 5")
print("Statement 6")

"""output:
Statement 1
Statement 5
Abnormal termination
"""
#Case 4:If there is exception at st 2
try:
    print("Statement 1")
    print(10/0)#statement 2 : exception
    print("statement 3")
except TypeError:
    print("Statement 4")
except ZeroDivisionError:
    print("Zero Division is not possible")
finally:
    print("Statement 5")
print("Statement 6")

"""output:
Statement 1
Zero Division is not possible
Statement 5
Statement 6

#Case 4:If there is exception at st 2, and exception is in except block
try:
    print("Statement 1")
    print(10/0)#statement 2 : exception
    print("statement 3")
except TypeError:
    print("Statement 4")
except ZeroDivisionError:
    print(2/0)#Exception
finally:
    print("Statement 5")
print("Statement 6")

"""output:
Statement 1
Statement 5
Abnormal Termination
"""
Note: Exception is at statement 2 and also in except block, in addition, to try block, there may be a chance of raising exception inside except and finally block also, it leads to abnormal termination.
"""
#Case 5:If there is no exception in try block
try:
    print("Statement 1")
    print(10/2)#statement 2
    print("statement 3")
except TypeError:
    print("Statement 4")
except ZeroDivisionError:
    print(2/0)#Exception
finally:
    print("Statement 5")
print("Statement 6")

"""output:
Statement 1
5.0
statement 3
Statement 5
Statement 6
"""
#Case 6:exception is raised out side the try block, which leads to abnormal
#termination
try:
    print("Statement 1")
    print(10/2)#statement 2
    print("statement 3")
except TypeError:
    print("Statement 4")
except ZeroDivisionError:
    print("Statement 5")
finally:
    print("Statement 6")
print(10/0)#st 7

"""output:
Statement 1
5.0
statement 3
Statement 6
Abnormal termination
"""
#Multiple except blocks
try:
    a=int(input("enter a value"))
    b=int(input("enter b value"))
    print(a/b)
except ZeroDivisionError:
    print("b=",b,"should not be zero")
except ValueError:
    print("Enter integer only")
"""output:
Case1:
enter a value4
enter b value2
2.0
Case 2:
enter a value4
enter b value0
b= 0 should not be zero
Case 3:
enter a value4
enter b valuetwo
Enter integer only
"""
Combination except blocks also can be written
#Multiple except blocks
try:
    a=int(input("enter a value"))
    b=int(input("enter b value"))
    print(a/b)
except (ZeroDivisionError,ValueError):
    print("b=",b,"should not be zero")
    print("Enter integer only")

"""output:
Case1:
enter a value4
enter b value2
2.0
Case 2:
enter a value4
enter b value0
b= 0 should not be zero
Case 3:
enter a value4
enter b valuetwo
Enter integer only
"""
Default exception:
We can use default except block to handle any type of exceptions.
#default except
try:
    a=int(input("enter a value"))
    b=int(input("enter b value"))
    print(a/b)
except ZeroDivisionError:
    print("b=",b,"should not be zero")
except: #default exception
    print("Enter integer only")

"""output:
Case1:
enter a value4
enter b value2
2.0
Case 2:
enter a value4
enter b value0
b= 0 should not be zero
Case 3:
enter a value4
enter b valuetwo
Enter integer only
"""
    Related Video: https://youtu.be/dq9W6ig91QQ

Next: Exception Handling 3
Prev:Exception Handling part1
https://youtu.be/oC6v7WLzZFw

5 comments:

  1. Strange "water hack" burns 2 lbs in your sleep

    More than 160 000 women and men are trying a easy and secret "liquid hack" to drop 1-2lbs each night in their sleep.

    It's painless and it works with anybody.

    Here's how you can do it yourself:

    1) Go grab a clear glass and fill it with water half full

    2) Then follow this proven HACK

    and you'll be 1-2lbs lighter as soon as tomorrow!

    ReplyDelete
  2. Did you know there's a 12 word phrase you can communicate to your man... that will induce intense feelings of love and instinctual appeal to you deep within his chest?

    That's because hidden in these 12 words is a "secret signal" that triggers a man's impulse to love, worship and protect you with his entire heart...

    12 Words Who Trigger A Man's Desire Instinct

    This impulse is so built-in to a man's mind that it will drive him to work harder than ever before to love and admire you.

    In fact, triggering this dominant impulse is absolutely important to getting the best ever relationship with your man that once you send your man one of these "Secret Signals"...

    ...You will instantly notice him open his soul and heart for you in a way he's never expressed before and he'll identify you as the one and only woman in the universe who has ever truly attracted him.

    ReplyDelete
  3. This is such a great resource that you are providing and you give it away for free. This is really a nice and informative, containing all information and also has a great impact on the new technology. We are technology/news/smartphone company, If you want to read such useful news then, Visit us: https://techmie.com/


    ReplyDelete
  4. This information is really awesome thanks for sharing most valuable information.
    DevOps Training
    DevOps Online Training

    ReplyDelete
  5. Thank you for sharing such a nice and interesting blog with us.
    Nice Information
    Python Online Training Hyderabad
    Best Python Online Training

    ReplyDelete

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(...