-->We can write a single except block that can handle multiple different types of exceptions.
except (Exception1, Exception2, Exception3, ...):
or
except (Exception1, Exception2, Exception3, ...) as msg:
Parentheses are mandatory and this group of exceptions is internally considered as a tuple.
Example
try:
x = int(input("Enter First Number: "))
y = int(input("Enter Second Number: "))
print(x / y)
except (ZeroDivisionError, ValueError) as msg:
print("Plz Provide valid numbers only and problem is:", msg)
Output:
D:\Python3>py SingleEx.py
Enter First Number: 10
Enter Second Number: 0
Plz Provide valid numbers only and problem is: division by zero
D:\Python3>py SingleEx.py
Enter First Number: 10
Enter Second Number: ten
Plz Provide valid numbers only and problem is: invalid literal for int() with base 10: 'ten'
No comments:
Post a Comment
Thank you Very Much.For Given Comment