πŸ”₯ Welcome to my blog! Latest updates available now. Visit daily! πŸ”₯ πŸ”₯ Click Here for Latest Updates – Visit My Blog πŸ”₯
πŸ”₯ Latest Updates πŸ“Š GP Grade Finder πŸ“’ Government Services Info πŸ†• New Schemes Updates

Single except block that can handle multiple exceptions in Python

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

Quatation of the Day 22-03-2026