Skip to content

Commit fc9ed89

Browse files
Printing Exception message using 'as' keyword,Exception Handling using try and single exception block that can handle multiple exceptions and handling using default exception block.
1 parent 6e2e753 commit fc9ed89

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

ExceptionHandling2.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#printing Exception message using 'as' Keyword
2+
try:
3+
print(10/0)
4+
except ZeroDivisionError as msg:
5+
print("Exception ocurred and its cause is:",msg)
6+
7+
#Exception handling using try and single except block that can handle mutiple Exceptions
8+
9+
try:
10+
x=int(input("Enter value of x:"))
11+
y=int(input("Enter value of y:"))
12+
print("The Division is:",x/y)
13+
except (ZeroDivisionError,ValueError,ArithmeticError) as msg:
14+
print("Exception cause is:",msg)
15+
16+
#Default except block (It must be present at last if there are multiple except blocks are present)
17+
18+
try:
19+
x=int(input("Enter value of x:"))
20+
y=int(input("Enter value of y:"))
21+
print("The Division is:",x/y)
22+
except IndexError as msg:
23+
print("Exception caused by:",msg)
24+
except OverflowError as msg:
25+
print("Exception caused by:",msg)
26+
except TypeError as msg:
27+
print("Exception caused by :",msg)
28+
except:
29+
print("Exception caused by default exception block:")
30+

0 commit comments

Comments
 (0)