#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